您好,登錄后才能下訂單哦!
怎么在Thinkphp中利用smarty與uploadify實現(xiàn)一個無刷新上傳功能?針對這個問題,這篇文章詳細(xì)介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
模板文件代碼:
<!DOCTYPE html> <html lang="cn"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <link href="<{$smarty.const.PUBLIC_PATH}>/Uploadify/uploadify.css" rel="stylesheet" type="text/css" /> <script src="<{$smarty.const.PUBLIC_PATH}>/Uploadify/jquery.js" type="text/javascript"></script> <script src="<{$smarty.const.PUBLIC_PATH}>/Uploadify/jquery.uploadify.min.js" type="text/javascript"></script> </head> <script type="text/javascript"> $(function() { $("#file_upload").uploadify({ //指定swf文件 'swf': '<{$smarty.const.PUBLIC_PATH}>/Uploadify/uploadify.swf', //后臺處理的頁面 'uploader': "<{U('home/Login/Uploads','',false)}>", //按鈕顯示的文字 'buttonText': '上傳圖片', //顯示的高度和寬度 "height" : 30, 'fileTypeDesc': 'Image Files', //允許上傳的文件后綴 'fileTypeExts': '*.gif; *.jpg; *.png', //發(fā)送給后臺的其他參數(shù)通過formData指定 //'formData': { 'someKey': 'someValue', 'someOtherKey': 1 }, "method" : 'post',//方法,服務(wù)端可以用$_POST數(shù)組獲取數(shù)據(jù) 'removeTimeout' : 1, "onUploadSuccess" : uploadPicture }); //可以根據(jù)自己的要求來做相應(yīng)處理 function uploadPicture(file, data){ var data = eval('(' + data + ')'); if(data.errorcode){ alert(data.errormsg); } else { alert(data.errormsg); } } }); </script> <body> <input type="file" name="file_upload" id="file_upload" /> </body> </html>
控制器代碼:
public function uploads(){ $arr = array( "errorcode"=>"1","errormsg"=>"上傳成功!"); $model = M('applicant'); if (!empty($_FILES)) { //圖片上傳設(shè)置 $config = array( 'maxSize' => 1000000, 'rootPath' => 'Public', 'savePath' => '/Uploads/', 'saveName' => array('uniqid',''), 'exts' => array('jpg', 'gif', 'png', 'jpeg'), 'autoSub' => false, 'subName' => array('date','Ymd'), ); $upload = new \Think\Upload($config);// 實例化上傳類 $info = $upload->upload(); if($info){ $arr['errorcode'] = "0"; } else { $arr["errorcode"] = "1"; $arr["errormsg"] = $upload->getError(); } /* 返回JSON數(shù)據(jù) */ $this->ajaxReturn($arr); } }
關(guān)于怎么在Thinkphp中利用smarty與uploadify實現(xiàn)一個無刷新上傳功能問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注億速云行業(yè)資訊頻道了解更多相關(guān)知識。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。