詳解thinkphp ajaxfileupload異步上傳圖片

下面由thinkphp教程欄目給大家介紹thinkphp ajaxfileupload異步上傳圖片方法,希望對需要的朋友有所幫助!

詳解thinkphp ajaxfileupload異步上傳圖片

thinkphp ajaxfileupload 異步上傳圖片

thinkphp開發(fā)圖片上傳,圖片異步上傳是目前比較方便的功能,這里我就不寫css文件了,將代碼寫出來。

html

下面首先在html頁面引入相關(guān)JS資源

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>圖片上傳</title>  <script type="text/JavaScript" src="js/jquery-1.8.2.min.js"></script> <script type="text/javascript" src="js/ajaxfileupload.js"></script>    </head> <body> </body> </html>

接下來在body中創(chuàng)建相關(guān)p

<label class="title w100">封面圖片:</label> <p class="f_l">     <label class="fileupload" onclick="upd_file(this,'image_file');">         <input type="file" class="filebox" name="image_file" id="image_file"/>         <!--上傳成功后圖片會給value賦值圖片路徑,以便于form表單提交數(shù)據(jù)-->         <input type="hidden" name="image" value="">                             </label>     <label class="fileuploading hide" ></label>                     </p> <p class="blank15"></p> <!--上傳成功后圖片會在這里顯示否則是默認(rèn)圖片--> @@##@@

解釋一下:
其中upd_file(this,’image_file’)不可缺少
其中隱藏的input 是用于上傳成功后賦值圖片路徑,以便于form表單提交數(shù)據(jù)
接下來在html中編輯javascript腳本以便于傳遞和提交圖片功能

<script> function upd_file(obj,file_id){     $("input[name='"+file_id+"']").bind("change",function(){                 $(obj).hide();     $(obj).parent().find(".fileuploading").removeClass("hide");     $(obj).parent().find(".fileuploading").removeClass("show");     $(obj).parent().find(".fileuploading").addClass("show");       $.ajaxFileUpload        (            {                 url:'/index.php/home/avatar/app_upload_image',//上傳圖片處理文件                 secureuri:false,                 fileElementId:file_id,                 dataType: 'json',                 success: function (data, status)                 {                        $(obj).show();                        $(obj).parent().find(".fileuploading").removeClass("hide");                     $(obj).parent().find(".fileuploading").removeClass("show");                     $(obj).parent().find(".fileuploading").addClass("hide");                        if(data.status==1)                        {                            $("#image").attr("src",data.thumb_url+"?r="+Math.random());                                                           $("input[name='image']").val(data.url);//返回json后將隱藏input賦值                         //$("#img_url").html('<input type="hidden" name="img_url" value="'+ path.path +'" />');                        }                        else                        {                            $.showErr(data.msg);                        }                 },                 error: function (data, status, e)                 {                     $.showErr(data.responseText);;                     $(obj).show();                     $(obj).parent().find(".fileuploading").removeClass("hide");                     $(obj).parent().find(".fileuploading").removeClass("show");                     $(obj).parent().find(".fileuploading").addClass("hide");                 }            }        );       $("input[name='"+file_id+"']").unbind("change"); });     } <script>

thikphp 中創(chuàng)建方法 app_upload_image()

    function app_upload_image($maxSize=52428800){         $id=session('id');         $config=array(             'rootPath'  =>'Upload',         //文件上傳保存的根路徑             'savePath'  =>'/avatar/',                'exts'      => array('jpg', 'gif', 'png', 'jpeg','bmp'),             'maxSize'   => $maxSize,             'autoSub'   => true,             );         $upload = new ThinkUpload($config);// 實例化上傳類         $z = $upload->uploadOne($_FILES['image_file']);         if($z) {         //拼接圖片的路徑名                 $img='/Upload'.$z['savepath'].$z['savename'];                 $_POST['image_file']=$img;                 //獲取上傳圖片絕對路徑                 $imgsrc=$_SERVER['DOCUMENT_ROOT'].__ROOT__.$_POST['image_file'];                 $image = new ThinkImage();                  $image->open($imgsrc);                 //將圖片裁剪為400x400并保存為corp.jpg                 $image->thumb(205, 160,ThinkImage::IMAGE_THUMB_CENTER)->save($imgsrc);              $this->ajaxReturn(array("thumb_url"=>$img,"url"=>$img,"status"=>1));         }     }

OK這樣就好了,首先和大家說一下,如果ajaxfileupload.js報錯程序是不會跑通的,如果你的程序報錯就檢查你的ajaxfileupload文件是不是版本的問題。

相關(guān)推薦:最新的10個thinkphp視頻教程

詳解thinkphp ajaxfileupload異步上傳圖片

? 版權(quán)聲明
THE END
喜歡就支持一下吧
點贊14 分享