jquery 多个Ajax请求
此代码段允许将多个文件拖动到框中。 FileReader会创建每个文件的斑点,然后使用AJAX RQ:
$.each( e.dataTransfer.files, function(index, file){
var fileReader = new FileReader();
//..generate BLOB from file
fileReader.onloadend = (function(file) {
return function(e) {
uploadfilelist.append('<li>' + file.fileName + '</li>');
//send every single file to server
var destfoldername=CURRENTPATH;
var uploadfilename=file.fileName;
var fd = new FormData();
fd.append("param1", destfoldername);
fd.append("param2", uploadfilename);
fd.append("param3", blob);
$.ajax({
type:"POST",
url:"url",
data:fd,
contentType: false,
processData: false,
beforeSend:function (xhr){
//custom headers
},
success: function(data, textStatus, jqXHR){
},
complete: function(jqXHR){
alert("State after complete: " + jqXHR.statusText);
}
});
};
})(file);
fileReader.readAsBinaryString(file);
}
QUASSAL:问题: 当没有处理前一个斑点时,服务器内部崩溃时会崩溃。
我找到了另一篇讨论这一点的文章:如何使所有ajax调用sequential? 使用async:false不是一个选项的“顺序”请求,它将阻止许多其他内容。
解决方案:??? 致电ajax forfile1,呼叫完成后,请致电file2,...致电ajax for file-n,
我真的很想使用jq deferred( http://api.jquery.com/category/deferred-object/ ) 例如,如下所述:
$.when($.ajax(???how to refer to THIS ajax call).done(function(){
//call this ajax for the next file again or use $.ajax(code).then()?
});
我真的很抱歉,但我不知道该如何正确。
感谢您的建议! h。
This code snippet allows to drop multiple files dragged into a box. The filereader creates a blob of each file and then EACH file should be send to server using the ajax rq:
$.each( e.dataTransfer.files, function(index, file){
var fileReader = new FileReader();
//..generate BLOB from file
fileReader.onloadend = (function(file) {
return function(e) {
uploadfilelist.append('<li>' + file.fileName + '</li>');
//send every single file to server
var destfoldername=CURRENTPATH;
var uploadfilename=file.fileName;
var fd = new FormData();
fd.append("param1", destfoldername);
fd.append("param2", uploadfilename);
fd.append("param3", blob);
$.ajax({
type:"POST",
url:"url",
data:fd,
contentType: false,
processData: false,
beforeSend:function (xhr){
//custom headers
},
success: function(data, textStatus, jqXHR){
},
complete: function(jqXHR){
alert("State after complete: " + jqXHR.statusText);
}
});
};
})(file);
fileReader.readAsBinaryString(file);
}
PROBLEM:
the server internal crashes when receiving a next blob while not having processed the former one.
I found another Post discussing this: How to make all AJAX calls sequential?
A "sequential" Request using async:false is not an option, it would block a whole lot of other things..
SOLUTION: ???
Call ajax forfile1, when call is done, call ajax for file2, ...call ajax for file-n
I would really like to use JQ Deferred (http://api.jquery.com/category/deferred-object/)
e.g. as described here: http://api.jquery.com/jQuery.when/
$.when($.ajax(???how to refer to THIS ajax call).done(function(){
//call this ajax for the next file again or use $.ajax(code).then()?
});
I am really sorry, but I do not know how to get it right.
Thanks for any suggestions!
h.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那么有一个插件队列插件:
http://plugins.jquery.com/project/ajaxqueue
由 John Resig 本人撰写。
Well there is a plugin the queue plugin:
http://plugins.jquery.com/project/ajaxqueue
It's written by John Resig himself.
当然,您可以通过使用下一个承诺来解决,从而在没有插件的情况下对它们进行排队,
这几乎与嵌套回调相同,并且具有可读性和捕获所有错误的额外好处
you can certainly queue them without a plugin by resolving with the next promise
this is almost the same thing as nesting the callbacks with the added benefit of readability and the catch all for errors