本文实例讲述了ThinkPHP结合html' target='_blank'>AjaxFileUploader实现无刷新文件上传的方法。分享给大家供大家参考。具体实现方法分析如下:首先,AjaxFileUploader插件是一个基于jquery的插件,我们可以使用AjaxFileUploader插件来实现文件异步上传功能了,使用这款插件上传文件不要担心兼容性的问题,它的兼容性可以说兼容所有主流浏览器,下面来给大家介绍一个AjaxFileUploader+thinkphp实现文件上传的实例。ThinkPHP框架下用AjaxFileUploader插件实现ajax文件上传,支持多种文件格式,页面无刷新上传。在Lib/Action/目录下创建upAction.class.php文件,代码如下:复制代码 代码如下: php class upAction extends BaseAction{ public function index(){ $this- display(); }
/* *@文件上传 *@author FineYi *@date 2013-01-23 */ public function upLoadFile(){ $error = ""; $msg = ""; $fileElementName = 'fileToUpload'; if(!empty($_FILES[$fileElementName]['error'])){ switch($_FILES[$fileElementName]['error']){ case '1': $error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; break; case '2': $error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; break; case '3': $error = 'The uploaded file was only partially uploaded'; break; case '4': $error = 'No file was uploaded.'; break;
case '6': $error = 'Missing a temporary folder'; break; case '7': $error = 'Failed to write file to disk'; break; case '8': $error = 'File upload stopped by extension'; break; case '999': default: $error = 'No error code avaiable'; } }elseif(empty($_FILES['fileToUpload']['tmp_name']) || $_FILES['fileToUpload']['tmp_name'] == 'none'){ $error = 'No file was uploaded..'; }else{ $re = $this- up(); if(!$re){ $error = 'Up file fail'; } $msg = $re['savename']; //文件名 $path = '/upload/bizcoop/'.$msg; //文件路径 $size = $re['size']; //文件大小 } echo json_encode(array('error'= $error,'msg'= $msg,'path'= $path,'size'= $size));exit; }
private function up(){ import('@.Org.UploadFile');//将上传类UploadFile.class.php拷到Lib/Org文件夹下 $upload=new UploadFile();