###
利用formData传递参数,xhr.upload.progress 方法得到上传进度 并清空input:file 当前值,可以重复上传文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| $('.file-btn').on('change', '#fileupload', function(e){ var file = $(this)[0].files //files 文件列表
for ( var i=0,len=file.length; i<len; i++ ){ var formData = new FormData(); formData.append('file', file[i]); $.ajax({ url: 'upload', // upload url type:'post', data: formData, // 参数 contentType: false, processData: false, xhr: function() //xhr对象,得到客户端发出去的进度 { var xhr = new XMLHttpRequest(); xhr.upload.addEventListener("progress", function(evt){ if (evt.lengthComputable) { var percentComplete = parseInt(evt.loaded * 100 / evt.total); } }, false); return xhr; }, success: function(data){ // 上传接口 回调处理 // code } }); }
// 清空当前的input:file值,可以连续上传同名文件 var newDom = $(this).clone().val(''); $(this).remove(); $('.file-btn').append(newDom); });
|