您好,登錄后才能下訂單哦!
如何在ThinkPHP中利用AjaxFileUploader實現(xiàn)一個無刷新文件上傳功能?相信很多沒有經(jīng)驗的人對此束手無策,為此本文總結了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個問題。
首先,AjaxFileUploader插件是一個基于jquery的插件,我們可以使用AjaxFileUploader插件來實現(xiàn)文件異步上傳功能了,使用這款插件上傳文件不要擔心兼容性的問題,它的兼容性可以說兼容所有主流瀏覽器,下面來給大家介紹一個AjaxFileUploader+thinkphp實現(xiàn)文件上傳的實例。
ThinkPHP框架下用AjaxFileUploader插件實現(xiàn)ajax文件上傳,支持多種文件格式,頁面無刷新上傳。
在Lib/Action/目錄下創(chuàng)建upAction.class.php文件,代碼如下:
<?phpclass 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(); $upload->maxSize='-1';//默認為-1,不限制上傳大小 $upload->savePath= ICTSPACE_DIST_ROOT_PATH.'/www/upload/bizcoop/';//保存路徑 $upload->saveRule=uniqid;//上傳文件的文件名保存規(guī)則 $upload->uploadReplace=true;//如果存在同名文件是否進行覆蓋 $upload->allowExts=array('jpg','jpeg','png','gif');//準許上傳的文件類型 if($upload->upload()){ $info=$upload->getUploadFileInfo(); return $info[0]; }else{ return false; exit; } }}?>
在/Tpl/default/Up/目錄下創(chuàng)建index.tpl文件,代碼如下:
<div id="content"> <h2>Ajax File Upload Demo</h2> <img id="loading" alt="" src="__APP____PUBLIC__/style/img/loading.gif" /> <form action="" enctype="multipart/form-data" method="POST" name="form"> <table class="tableForm" cellspacing="0" cellpadding="0"> <thead> <tr> <th></th> </tr> </thead> <tbody> <tr> <td><input class="input" id="fileToUpload" type="file" name="fileToUpload" size="45" /></td> </tr> <tr> <td><button class="button" id="buttonUpload" onclick="return ajaxFileUpload();">Upload</button></td> </tr> </tbody> <tbody> <tr> <td><span>已上傳的附件:</span></td> </tr> </tbody> <tfoot></tfoot> </table> </form></div>
在/Lib/Org/目錄下放入ThinkPHP文件上傳類就可以了
看完上述內(nèi)容,你們掌握如何在ThinkPHP中利用AjaxFileUploader實現(xiàn)一個無刷新文件上傳功能的方法了嗎?如果還想學到更多技能或想了解更多相關內(nèi)容,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。