国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 語(yǔ)言 > PHP > 正文

thinkphp ajaxfileupload實(shí)現(xiàn)異步上傳圖片的示例

2024-05-04 23:59:51
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

thinkphp開發(fā)圖片上傳,圖片異步上傳是目前比較方便的功能,這里我就不寫css文件了,將代碼寫出來(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í)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到PHP教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 萨嘎县| 延川县| 三门县| 泗阳县| 绥滨县| 沭阳县| 泸西县| 吉安市| 汾西县| 翁牛特旗| 鄂托克前旗| 舒兰市| 梧州市| 台南县| 盐源县| 页游| 和林格尔县| 上栗县| 浮山县| 武宣县| 铜山县| 乐东| 大港区| 杭锦后旗| 绥中县| 儋州市| 卓尼县| 全南县| 汝州市| 鹿泉市| 鸡泽县| 旅游| 军事| 泾川县| 吴桥县| 兴义市| 镇远县| 霸州市| 普陀区| 九寨沟县| 巴青县|