thinkphp開(kāi)發(fā)圖片上傳,圖片異步上傳是目前比較方便的功能,這里我就不寫(xiě)css文件了,將代碼寫(xiě)出來(lái)。引入核心文件下載https://github.com/carlcarl/A...
HTML
下面首先在html頁(yè)面引入相關(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>
接下來(lái)在body中創(chuàng)建相關(guān)div
<label class="title w100">封面圖片:</label> <div class="f_l"> <label class="fileupload" onclick="upd_file(this,'image_file');"> <input type="file" class="filebox" name="image_file" id="image_file"/> <!--上傳成功后圖片會(huì)給value賦值圖片路徑,以便于form表單提交數(shù)據(jù)--> <input type="hidden" name="image" value=""> </label> <label class="fileuploading hide" ></label> </div> <div class="blank15"></div> <!--上傳成功后圖片會(huì)在這里顯示否則是默認(rèn)圖片--> <img id="image" src="/Public/images/empty_thumb.gif" />
解釋一下:
其中upd_file(this,'image_file')不可缺少
其中隱藏的input 是用于上傳成功后賦值圖片路徑,以便于form表單提交數(shù)據(jù)
接下來(lái)在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 \Think\Upload($config);// 實(shí)例化上傳類 $z = $upload->uploadOne($_FILES['image_file']); if($z) { //拼接圖片的路徑名 $img='/Upload'.$z['savepath'].$z['savename']; $_POST['image_file']=$img; //獲取上傳圖片絕對(duì)路徑 $imgsrc=$_SERVER['DOCUMENT_ROOT'].__ROOT__.$_POST['image_file']; $image = new \Think\Image(); $image->open($imgsrc); //將圖片裁剪為400x400并保存為corp.jpg $image->thumb(205, 160,\Think\Image::IMAGE_THUMB_CENTER)->save($imgsrc); $this->ajaxReturn(array("thumb_url"=>$img,"url"=>$img,"status"=>1)); } }
OK這樣就好了,首先和大家說(shuō)一下,如果ajaxfileupload.js報(bào)錯(cuò)程序是不會(huì)跑通的,如果你的程序報(bào)錯(cuò)就檢查你的ajaxfileupload文件是不是版本的問(wèn)題
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持。
【網(wǎng)站聲明】本站除付費(fèi)源碼經(jīng)過(guò)測(cè)試外,其他素材未做測(cè)試,不保證完整性,網(wǎng)站上部分源碼僅限學(xué)習(xí)交流,請(qǐng)勿用于商業(yè)用途。如損害你的權(quán)益請(qǐng)聯(lián)系客服QQ:2655101040 給予處理,謝謝支持。