PHP+Ajax實現無重新整理帶進度條圖片上傳

來源:互聯網
上載者:User
本篇文章主要介紹了PHP+Ajax無重新整理帶進度條圖片上傳樣本,詳細的整理PHP無重新整理上傳圖片,並且要帶進度條的代碼,有需要的可以瞭解一下。

項目需求:1.PHP+Ajax無重新整理帶進度條圖片上傳,2.帶進度條。所需外掛程式:jquery.js,jquery.form.js。

最近在做一個手機web項目,需要用到Ajax上傳功圖片能,項目要求PHP無重新整理上傳圖片,並且要帶進度條,下面就來講一下我的實現方法,先看

本樣本需要使用的是jquery.js,jquery.form.js,demo裡麵包含有,你可以在文章下方進行下載。

第一步,建立前端頁面index.html

此段是前端展示內容,這裡需要說明的是由於input:file標籤顯示不太美觀,所以我把它隱藏了。而使用一個a標籤.uploadbtn來調用file標籤的click事件,用來開啟並選擇檔案。

注意:檔案上傳時form的屬性enctype必須設定為:multipart/form-data

<!DOCTYPE HTML><html><head><meta charset="UTF-8"><title>php-ajax無重新整理上傳(帶進度條)demo</title><meta name="description" content="" /><meta name="viewport" content="width=device-width , initial-scale=1.0 , user-scalable=0 , minimum-scale=1.0 , maximum-scale=1.0" /><script type='text/javascript' src='js/jquery-2.0.3.min.js'></script><script type='text/javascript' src='js/jquery.form.js'></script><link href="css/style.css" rel="external nofollow" type="text/css" rel="stylesheet"/></head><body><p style="width:500px;margin:10px auto; border:solid 1px #ddd; overflow:hidden; "> <form id='myupload' action='upload.php' method='post' enctype='multipart/form-data'> <input type="file" id="uploadphoto" name="uploadfile" value="請點擊上傳圖片" style="display:none;" /> </form> <p class="imglist"> </p> <p class="res"></p> <p class="progress"> <p class="progress-bar progress-bar-striped"><span class="percent">50%</span></p> </p> <a href="javascript:void(0);" rel="external nofollow" onclick="uploadphoto.click()" class="uploadbtn">點擊上傳檔案</a></p></body></html>

第二步,Ajax提交部分

這部份就是Ajax的提交部份,過程如下:

  • 在提交開始通過beforeSend回呼函數設定進度條顯示出來,進度條寬度為0%,進度值0%;

  • 在上傳過程中通過uploadProgress回呼函數即時返回的資料,更改進度條的寬度和進度值。

  • 在上傳成功後,通過success回呼函數輸出上傳為資料資訊(圖片名稱,大小,地址等)並把圖片輸出到頁面上預覽。

  • 當然如果失敗,有error回呼函數幫你進行高度。

<script type="text/javascript">$(document).ready(function(e) { var progress = $(".progress");  var progress_bar = $(".progress-bar"); var percent = $('.percent'); $("#uploadphoto").change(function(){ $("#myupload").ajaxSubmit({  dataType: 'json', //資料格式為json  beforeSend: function() { //開始上傳  progress.show(); var percentVal = '0%'; progress_bar.width(percentVal); percent.html(percentVal); },  uploadProgress: function(event, position, total, percentComplete) {  var percentVal = percentComplete + '%'; //獲得進度  progress_bar.width(percentVal); //上傳進度條寬度變寬  percent.html(percentVal); //顯示上傳進度百分比  },  success: function(data) {  if(data.status == 1){ var src = data.url;  var attstr= '<img src="'+src+'">';  $(".imglist").append(attstr); $(".res").html("上傳圖片"+data.name+"成功,圖片大小:"+data.size+"K,檔案地址:"+data.url); }else{ $(".res").html(data.content); } progress.hide();  },  error:function(xhr){ //上傳失敗  alert("上傳失敗");  progress.hide();  }  });  }); });</script>

第三步,後端PHP代碼upload.php

後端處理代碼,就是PHP檔案上傳,不過上傳的時候需要做一些判斷,如檔案格式、檔案大小等。

注意:我上面ajax返回格式是json,所以在圖片json代碼是一定要正確規範,否則會出現上傳不成功的提示。

$picname = $_FILES['uploadfile']['name'];  $picsize = $_FILES['uploadfile']['size'];  if ($picname != "") {  if ($picsize > 2014000) { //限制上傳大小  echo '{"status":0,"content":"圖片大小不能超過2M"}'; exit;  }  $type = strstr($picname, '.'); //限制上傳格式  if ($type != ".gif" && $type != ".jpg" && $type != "png") { echo '{"status":2,"content":"圖片格式不對!"}'; exit;  } $rand = rand(100, 999);  $pics = uniqid() . $type; //命名圖片名稱  //上傳路徑  $pic_path = "images/". $pics;  move_uploaded_file($_FILES['uploadfile']['tmp_name'], $pic_path);  }  $size = round($picsize/1024,2); //轉換成kb  echo '{"status":1,"name":"'.$picname.'","url":"'.$pic_path.'","size":"'.$size.'","content":"上傳成功"}';

demo下載: php-ajax-upload_jb51.rar

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


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.