フロントエンドHTML+JQuery 注意 Jqueryはバージョン1.xが必要、バージョン2.xは使用できません
1.必要なファイルを導入し入力をアップロード
<load file="__PUBLIC__/js/jquery-1.11.3.min.js" /> <load file="__PUBLIC__/uploadify/jquery.uploadify.min.js" /> <load file="__PUBLIC__/uploadify/uploadify.css" />
<span><</span><span>input </span><span>type</span><span>="file"</span><span> class</span><span>=""</span><span> id</span><span>="student"</span><span> name</span><span>="student"</span><span>></span>
2.使用操作をアップロード
<script><span> $(</span><span>function</span><span>(){ </span><span>//</span><span>ThinkPHP上传地址</span><span>var</span> upload = "{:U(MODULE_NAME.'/Student/upload')}"<span>; </span><span>//</span><span>ThinkPHP sessionID的提交变量</span><span>//</span><span>'VAR_SESSION_ID'=>'session_id',在ThinkPHP/Conf/convention.php中进行配置,默认关闭的需要开启</span><span>var</span> sid = '{:session_id()}'<span>; </span><span>//</span><span>导入数据地址</span><span>var</span> daoruUrl = "{:U(MODULE_NAME.'/Student/daoruHandle')}" <span>//</span><span>Amaze ui 模态框</span><span>var</span> modal = $('#my-modal-loading'<span>); </span><span>//</span><span>uploadify使用方法</span> $('#student'<span>).uploadify({ </span>'swf' : '__PUBLIC__/uploadify/uploadify.swf'<span>, </span>'uploader'<span> : upload, </span>'buttonText' : '选择文件...'<span>, </span>'width':120<span>, </span>'height':30<span>, </span>'formData':{'session_id'<span>:sid}, </span>'fileTypeExts':'*.xls'<span>, </span><span>//</span><span>开始上传弹出模态框</span> 'onUploadStart' : <span>function</span><span>(file) { $(</span>'#alert-content').html('正在<strong>上传文件</strong>'<span>); modal.modal(); }, </span><span>//</span><span>上传成功关闭模态框,并用后台函数导入数据</span> 'onUploadSuccess':<span>function</span><span>(file, data, response){ $(</span>'#alert-content').html('正在导入数据'<span>); data </span>= eval("("+data+")"<span>); $.<strong>ajax</strong>({ type: </span>'POST'<span>, url: daoruUrl, data: {</span>'file'<span>:data.file}, success: </span><span>function</span><span>(retdata){ modal.modal(</span>'close'<span>); </span><span>if</span>(retdata==1<span>){ alert(</span>'导入成功'<span>); }</span><span>else</span><span>{ alert(</span>'导入失败'<span>); } }, dataType: </span>'json'<span> }); } }); }); </span></script>
3. ThinkPHP コントローラーのアップロード操作: Upload.class.php スペースを導入する必要があることに注意してください
<span>function</span><span> upload(){ </span><span>$config</span> = <span>array</span><span>( </span>'maxSize' => 3145728, 'rootPath' => './Uploads/', 'savePath' => '', 'saveName' => <span>array</span>('uniqid',''), 'exts' => <span>array</span>('xls'), 'autoSub' => <span>true</span>, 'subName' => <span>array</span>('date','Ymd'),<span> ); </span><span>$upload</span> = <span>new</span> Upload(<span>$config</span><span>); </span><span>//</span><span> <strong>上传文件</strong> </span><span>$info</span> = <span>$upload</span>-><span>upload(); </span><span>if</span>(!<span>$info</span>) {<span>//</span><span> 上传错误提示错误信息</span><span>$this</span>->error(<span>$upload</span>-><span>getError()); }</span><span>else</span>{<span>//</span><span> 上传成功 获取<strong>上传文件</strong>信息</span><span>$file</span> = <span>$info</span>['Filedata']['savepath'].<span>$info</span>['Filedata']['savename'<span>]; } </span><span>//</span><span>p($info);</span><span>$data</span> = <span>array</span><span>( </span>'file'=>'./Uploads/'.<span>$file</span>,<span> ); </span><span>echo</span> json_encode(<span>$data</span><span>); }</span>
4. データを mysql にインポートします
<span>//</span><span>导入数据处理</span><span>function</span><span> daoruHandle(){ </span><span>$file</span> = I('file'<span>); </span><span>$excelData</span> = excel_to_mysql(<span>$file</span><span>); </span><span>foreach</span>(<span>$excelData</span>['data'] <span>as</span><span>$row</span><span>){ </span><span>$data</span> = <span>array</span><span>( </span>'xuehao'=><span>$row</span>['xuehao'], 'xingming'=><span>$row</span>['xingming'], 'xingbie'=>(<span>$row</span>['xingbie']=='男')?1:0, 'mima'=><span>md5</span>(<span>$row</span>['mima']),<span> ); M(</span>'student')->add(<span>$data</span><span>); } </span><span>echo</span> 1<span>; }</span>
5. PHPExcel は Excel ファイルを読み取り、データ関数を返します
れーれー
以上、更新せずにデータをインポートする ThinkPHP+uploadify+upload+PHPExcel を内容も含めて紹介しましたので、PHP チュートリアルに興味のある友人の参考になれば幸いです。