JavaScript File分段上傳_javascript技巧

來源:互聯網
上載者:User

HTML

<form method="POST" name="form" action="/mupload/upload/" enctype="multipart/form-data"><input type='hidden' name='csrfmiddlewaretoken' value='' /><input id='file' type='file' name='file' onchange="" /><input id='button' name='submit' type='button' value="上傳" onclick="chunk_upload(this)"/></form> 

js方法

var fileSplitSize = 1024 * 1024;var start=0,end=0;var i=0;// 檔案段上傳function chunk_upload(button){var xmlhttp = new XMLHttpRequest();xmlhttp.open("POST", "/chunk_upload/upload/", false);xmlhttp.setRequestHeader("X-CSRFToken", button.form['csrfmiddlewaretoken'].value);var f = button.form;var file = f['file']['files'][0];var size=file.size;end=start+fileSplitSize;if(end>size){i=-1;end=size;}else{i+=1;end=end;}<br>  //按大小切割檔案段var blob = file.slice(start, end);xmlhttp.setRequestHeader('charset','utf-8');xmlhttp.setRequestHeader("fileMD5", fileMD5);xmlhttp.setRequestHeader("start", start);xmlhttp.setRequestHeader("end", end);xmlhttp.send(blob);if(xmlhttp.status==200){<br> if(end==size){<br> var backtext=xmlhttp.responseText;<br> alert(backtext);<br> }else{<br> alert("上傳完成第"+i+"段")<br> start=end;<br> chunk_upload(button);<br> }<br> }else{<br> alert("上傳錯誤");<br> chunk_upload(button);<br> }} 

主要思想:

  注意設定切割的起始位置和切割大小,通過XMLHttpRequest的發送請求(http協議要知道)。

  如果一些標記資料可以添加協議頭:xmlhttp.setRequestHeader("end", end);

  發送協議體xmlhttp.send(data);

  監聽返回碼來判斷是否傳遞成功,在進行下一步操作。

  重新設定切割位置,然後遞迴調用自身start=end;chunk_upload(button);

注意:

  切割的start與end和filesize的關係

純js非同步上傳檔案,並返回上傳進度

純js實現非同步上傳檔案,非同步返迴文件上傳進度,0.05到0.1秒回調一次上傳進度,其它詳細見程式碼片段下用法注釋

1. 簡單的非同步上傳函數

;(function(window,document){var myUpload = function(option) {var file,fd = new FormData(),xhr = new XMLHttpRequest(),loaded, tot, per, uploadUrl,input;input = document.createElement("input");input.setAttribute('id',"myUpload-input");input.setAttribute('type',"file");input.setAttribute('name',"files");input.click();uploadUrl = option.uploadUrl;callback = option.callback;uploading = option.uploading;beforeSend = option.beforeSend;input.onchange= function(){file = input.files[0];if(beforeSend instanceof Function){if(beforeSend(file) === false){return false;}}fd.append("files", file);xhr.onreadystatechange = function() {if (xhr.readyState == 4 && xhr.status == 200) {if(callback instanceof Function){callback(xhr.responseText);}}}//偵查當前附件上傳情況xhr.upload.onprogress = function(evt) {loaded = evt.loaded;tot = evt.total;per = Math.floor(100 * loaded / tot); //已經上傳的百分比if(uploading instanceof Function){uploading(per);}};xhr.open("post", uploadUrl);xhr.send(fd);}};window.myUpload = myUpload;})(window,document);//用法//觸發檔案上傳事件myUpload({//上傳檔案接收地址uploadUrl: "/async/myUpload.php",//選擇檔案後,傳送檔案前自訂事件//file為上傳的檔案資訊,可在此處做檔案檢測、初始化進度條等動作beforeSend: function(file) {},//檔案上傳完成後回呼函數//res為檔案上傳資訊callback: function(res) {},//返回上傳過程中包括上傳進度的相關資訊//詳細請看res,可在此加入進度條相關代碼uploading: function(res) {}});

相關文章

聯繫我們

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