1次ajax請求(XMLHttpRequest)上傳多個檔案,支援進度條
封裝了一個ajax多檔案上傳,功能就是選擇多個檔案,用ajax上傳。
呼叫方式也很簡單,寫一個json物件做為引數配置,設定要上傳的服務端url以及選擇檔案和上傳完成等的事件處理函式,new一個AjaxUploadX物件,呼叫selectFiles方法選擇檔案,呼叫uplaod方法上傳。
支援的瀏覽器:Firefox、Chrome、Safari、Opera
使用截圖:
呼叫方法示例:
<button id="btnSelectFiles">選擇檔案</button> <button id="btnUpload">上傳</button> <ol id="filesView"></ol> <script> var config = { url : "saveupload.php", formname : "uploadedfile[]", onselectfiles : function (files){ var s = ''; for(var i=0, n=files.length;i<n;i++){ s += '<li>' + files[i].name + ' ('+files[i].size+' bytes)' + '</li>'; } document.getElementById("filesView").innerHTML = s; }, onload : function(e) { alert(e.target.responseText); }, uploadHandlers : { progress : function(e){} } }; var aux = new AjaxUploadX(config); document.getElementById("btnSelectFiles").onclick = function (){ aux.selectFiles(); } document.getElementById("btnUpload").onclick = function (){ aux.upload(); } </script>
封裝類的原始碼如下:
/* *---------------------------- * AjaxUploadX * 功能: 1次ajax請求(XMLHttpRequest)上傳多個檔案 * 作者: Igin Cui, 2012/8/20 * 聯絡我: @IginCui (新浪微博) * [email protected] * http://blog.csdn.net/cuixiping *---------------------------- */ /* 本例在Firefox 14, Chrome 20測試通過,未測試其他瀏覽器 */ /* IE9不支援files特性,所以本例不支援IE9 */ function AjaxUploadX(ops){ if(!window.FormData || !window.FileList){ //throw('Your browser does not support ajax upload'); throw('您的瀏覽器不支援ajax upload'); } this.options = ops||{}; this._init(); } AjaxUploadX.prototype = { _init: function (){ var ele = document.createElement('input'); ele.multiple=this.options.multiple!==false; ele.type='file'; ele.style.display='none'; document.body.appendChild(ele); var THIS = this; ele.onchange = function (e){ THIS._onchange(e); } this._input = ele; }, _destroy: function (){ document.body.removeChild(this._input); }, _onchange: function (e){ var ops = this.options; if(ops.onselectfiles){ ops.onselectfiles(e.target.files); } }, selectFiles: function (){ this._input.click(); }, upload: function (){ var xhr = new XMLHttpRequest(); var ops = this.options; var v, h, evs = {loadstart:0, loadend:0, progress:0, load:0, error:0, abort:0}; for(v in evs){ if(h = ops['on'+v]){ xhr.addEventListener(v, h, false); } if(ops['uploadHandlers'] && (h = ops['uploadHandlers']['on'+v])){ xhr.upload.addEventListener(v, h, false); } } var data = new FormData(); var files = this._input.files; if(!files.length){ this.options.onerror('No files'); return; } for(var i=0, n=files.length;i<n;i++){ data.append(ops.formname || "uploadedfile[]", files[i]); } xhr.open("POST", ops.url, true); xhr.send(data); } };
demo中包含封裝類,呼叫示例html,js,儲存上傳檔案的php.
新浪微博: @IginCui
~~
相關推薦
1次ajax請求(XMLHttpRequest)上傳多個檔案,支援進度條
封裝了一個ajax多檔案上傳,功能就是選擇多個檔案,用ajax上傳。 呼叫方式也很簡單,寫一個json物件做為引數配置,設定要上傳的服務端url以及選擇檔案和上傳完成等的事件處理函式,new一個AjaxUploadX物件,呼叫selectFiles方法選擇檔案,呼叫upla
vue上傳多個檔案,附件和其他資料一起傳給後臺
前端: 實現多圖上傳主要用到以下兩個屬性: <el-form-item label="附件上傳" label-width="80px"> <el-f
el-upload控制元件一次介面請求上傳多個檔案
el-upload元件預設情況下上傳多少個檔案就會請求多少次上傳介面,如何一次上傳多個檔案而不必多次請求上傳介面呢?直接看程式碼 html <el-upload :action="actionUrl" :auto-upload="false" :multiple="true" :file-list=
asp.net 中一次上傳多個檔案
看到一篇老外的文章,說在asp.net 中,如何先讓使用者把要上傳的檔案都選好了,然後一次上傳,今小結如下首先在頁面加一個上傳檔案控制元件,一個“attach"按鈕,一個listbox,用來存放等待上傳的檔名,一個"UPLOAD"按鈕,一個”刪除按鈕 <form
微信小程式上傳(多個檔案上傳)
微信小程式上傳(多個檔案上傳) /** * 上傳照片//選擇圖片時限制9張,如需超過9張,同理亦可參照此方法上傳多張照片 */ uploadImg:function(){ var that = this; wx.chooseImage({ count
ajaxfileupload.js 上傳多個檔案
上傳單個檔案 JS $.ajaxFileUpload({ url :"", secureuri : false, fileElementId : 'img', --傳入file ID data:
input型別上傳多個檔案(selenium+Python)
一般上傳方法分為兩種:input型別和非input型別,這裡具體分析input型別。 html檔案: 【上傳圖示】按鈕元素定位中包含name=“file” type="file" 果斷採用input+sendkeys模式。 一、上傳一張圖片,上程式碼: 解釋
php 在linux 同時上傳多個檔案時報錯 SplFileObject::__construct(/tmp/phpvfQPvQ):
分析:這個不是上傳檔案的最大值的問題,而是由於linux系統自動清除臨時檔案的願意造成的 thinkphp\library\think\File.php 的建構函式加個判斷 if(file_exists($filename)) 簡單解決報錯可以讓你用起來 不知道有沒有其他其他不
flask 伺服器上面下載多個檔案 ,客戶端一次性上傳多個檔案
#encoding=utf8 import flask,os,sys,time from flask import request,send_from_directory import multiEmbeddings import tensorflow as tf &n
asp.net簡單例項——同時上傳多個檔案
之前在網站上看到的一些上傳檔案的功能,感覺還是蠻方便的,這次自己利用asp.net中的HttpFileCollection類做了一個簡單的例子。廢話不多說,下面看操作。 首先,新增一個新的web窗體,在窗體上直接拖拽這幾個控制元件:一個Panel作為容器、
使用webuploader一次性上傳多個檔案
先下載webuploader的js包(http://fex.baidu.com/webuploader/download.html)然後是頁面中是jquery部分<script type="text/javascript">$(function(){/******
【微信小程式開發筆記】上傳多個檔案超過10個
【小程式筆記】wx.uploadFile(OBJECT) 先說說遇到的問題: 小程式可通過wx.uploadFile(OBJECT)介面上傳手機檔案至伺服器,但是在文件中關於請求中有這麼一段說明: request、uploadFile、down
Struts檔案上傳與下載詳解 _上傳多個檔案
在上一篇中給大家列出的單個檔案的上傳,那麼這一篇咱們講講上傳多個檔案改怎麼作呢?由於過程和上傳單個檔案的類似,所以在這裡不細說,相信大家都能看懂,看不懂的可以在評論區留言,我看到之後會及時
input 框上傳多個檔案以及檔案校驗
1、如何上傳多個檔案 在input標籤中加入 multiple 屬性,如下 <input type="file" name="file1" id="file1" multiple="multiple"/> 當然,這樣也是一樣的: <
使用FormData上傳多個檔案
由於專案中使用到,特此寫個Demo html程式碼: <html> <head> <title>Title</title> <s
單個檔案上傳+多個檔案上傳
單個檔案上傳 jsp頁面 <body> <form action="shangchuan.do" method="post" enctype="multipart/form-data"> 檔案<
jq上傳多張圖片(帶進度條)
<!DOCTYPE html><html> <head> <meta charset="UTF-8" /> <title>xhr2</title> </head> <body>&
input File實現同時上傳多個檔案
預設的呼叫方式: <input type="file" name="filename"/> 可實現上傳單個檔案,但無法多選確認。 html在input[type='file']中給我們提
關於在一個form表單裡同時上傳多個檔案和文字資訊的解決方案。。。
執行環境:tomcat5.0.30+springframework步驟:1。定義一個form表單index.jsp和error.jsp**************************************index.jsp************************
HttpClient傳送方實現上傳多個檔案
public static void upload(String url, List<String> filepaths,HashMap<String, String> mapParams) { HttpClient