php+ajax無重新整理上傳圖片的實現方法

來源:互聯網
上載者:User
這篇文章主要介紹了php+ajax無重新整理上傳圖片的實現方法,涉及php結合ajax進行檔案傳輸操作相關技巧,需要的朋友可以參考下

具體如下:

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部分

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

3.給fileupload加上表單

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

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', //資料格式為json      beforeSend: function() { //開始上傳        showimg.empty(); //清空顯示的圖片        imgsrc.val("");        btn.html("上傳中..."); //上傳按鈕顯示上傳中      },      uploadProgress: function(event, position, total, percentComplete) {      },      success: function(data) { //成功        //獲得後台返回的json資料,顯示檔案名稱,大小,以及刪除按鈕        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.後台進行處理

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.將返回的資料交給前端,進行一些處理。

進而提交到後台資料庫。

以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.