欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php+ajax無刷新上傳圖片實例代碼

 更新時間:2015年11月17日 14:56:18   作者:霍嘯林  
這篇文章為大家分享了php+ajax無刷新上傳圖片實例代碼,需要的朋友可以參考下

本文分享了php結(jié)合ajax實現(xiàn)無刷新上傳圖片的實例代碼,分享給大家,希望大家可以和小編一起學(xué)習(xí)學(xué)習(xí),共同進(jìn)步。

1.引入文件

<!--圖片上傳begin-->
<script type="text/javascript" src="/js/jquery.form.js"></script>
<script type="text/javascript" src="/js/uploadImg.js"></script>
<link href="/css/uploadImg.css" rel="stylesheet" type="text/css" />
<!--圖片上傳end-->

2.html部分

<div class="upimg">
       <input name="icon" type="text" class="imgsrc" value="<!--{$contents.icon}-->" />
       <div class="showimg">
        <!--{if $contents.icon}-->
        <img src="<!--{$contents.icon}-->" height="120px">
        <!--{/if}-->
       </div>          
       <div class="btn" style="height:20px;">
          <span>添加圖片</span>
          <input class="fileupload" type="file" name="pic[]">
       </div>
       </div> 

3.給fileupload加上表單

/*圖片上傳*/
  $(".fileupload").wrap("<form action='/bookstore/book/uploadpic' method='post' enctype='multipart/form-data'></form>"); //函數(shù)處理

4.ajax文件上傳

jQuery(function ($) { 
  $(".fileupload").change(function(){ //選擇文件 
    if ('' === $(this).val()) return;
    var upimg = $(this).parent().parent().parent();
    var showimg = upimg.find('.showimg');
    var btn = upimg.find('.btn span');
    var imgsrc = upimg.find('.imgsrc');
    $(this).parent().ajaxSubmit({ 
      //dataType: 'json', //數(shù)據(jù)格式為json 
      beforeSend: function() { //開始上傳 
        showimg.empty(); //清空顯示的圖片 
        imgsrc.val("");
        btn.html("上傳中..."); //上傳按鈕顯示上傳中 
      }, 
      uploadProgress: function(event, position, total, percentComplete) { 
      }, 
      success: function(data) { //成功 
        //獲得后臺返回的json數(shù)據(jù),顯示文件名,大小,以及刪除按鈕 
        var img = data;
        //顯示上傳后的圖片 
        imgsrc.val("");
        imgsrc.val(img);
        showimg.html("<img width='120' height='120' src='"+img+"'>"); 
        btn.html("上傳成功"); //上傳按鈕還原 
      }, 
      error:function(xhr){ //上傳失敗 
        btn.html("上傳失敗"); 
      } 
    }); 
  }); 
}); 

5.后臺進(jìn)行處理

public function uploadpicAction(){ //圖片上傳和顯示
    $data = "";
    $src = $this->uploadFiles2($imgpath = "/upload/book" ,$filesname = "pic");      
    isset($src[0]['src']) && $src[0]['src'] ? $data = $this->concaturl($src[0]['src']) : null;
    echo $data; 
  }

6.將返回的數(shù)據(jù)交給前端,進(jìn)行一些處理。

進(jìn)而提交到后臺數(shù)據(jù)庫。

希望本文所述對大家學(xué)習(xí)php程序設(shè)計有所幫助。

相關(guān)文章

最新評論