栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

HTML5多文件上传:通过AJAX一对一上传

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

HTML5多文件上传:通过AJAX一对一上传

为了使它成为同步操作,您需要在最后一次传输完成后开始新的传输。例如,Gmail可同时发送所有内容。AJAX文件上传进度事件是

progress
onprogress
在原始
XmlHttpRequest
实例上。

因此,在

$.ajax()
服务器端的每一个之后(我不知道您将使用什么),发送JSON响应以在下一个输入上执行AJAX。一种选择是将AJAX元素绑定到每个元素,以简化操作,因此您只需在
success
中即可
$(this).sibling('input').execute_ajax()

像这样:

$('input[type="file"]').on('ajax', function(){  var $this = $(this);  $.ajax({    'type':'POST',    'data': (new FormData()).append('file', this.files[0]),    'contentType': false,    'processData': false,    'xhr': function() {         var xhr = $.ajaxSettings.xhr();       if(xhr.upload){          xhr.upload.addEventListener('progress', progressbar, false);       }       return xhr;     },    'success': function(){       $this.siblings('input[type="file"]:eq(0)').trigger('ajax');       $this.remove(); // remove the field so the next call won't resend the same field    }  });}).trigger('ajax');  // Execute only the first input[multiple] AJAX, we aren't using $.each

上面的代码将用于多个,

<input type="file">
但不能用于
<input type="file" multiple>
,在这种情况下,应为:

var count = 0;$('input[type="file"]').on('ajax', function(){  var $this = $(this);  if (typeof this.files[count] === 'undefined') { return false; }  $.ajax({    'type':'POST',    'data': (new FormData()).append('file', this.files[count]),    'contentType': false,    'processData': false,    'xhr': function() {         var xhr = $.ajaxSettings.xhr();       if(xhr.upload){          xhr.upload.addEventListener('progress', progressbar, false);       }       return xhr;     },    'success': function(){       count++;       $this.trigger('ajax');    }  });}).trigger('ajax'); // Execute only the first input[multiple] AJAX, we aren't using $.each


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/393467.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号