【发布时间】:2014-03-25 11:22:33
【问题描述】:
这有点复杂:我有一个带有文件输入字段和下方上传按钮的表单。通过单击该上传按钮,将触发一个函数,该函数执行 ajax 调用以检查上传目录中是否已存在同名文件。如果文件不存在,则触发“start_upload”函数:
function start_upload(){
while(start < SIZE) { //start and end is 0 and ~200Bytes - that's the size of the parts of the file
i++;
upload(blob.slice(start, end), i, name, SIZE);
start = end;
end = start + BYTES_PER_CHUNK;
}
}
上传功能如下:
function upload(blobOrFile, part, name, size) {
var formdata = new FormData();
var xhr = new XMLHttpRequest();
formdata.append('blob', blobOrFile);
formdata.append('part', part);
formdata.append('filename', name);
xhr.open('POST', 'upload2.php', true);
xhr.onload = function (e) {
uploaders.pop();
if (!uploaders.length) {
//some code that's fired when upload has finished
}
};
//some other irrelevant functions
uploaders.push(xhr);
xhr.send(formdata);
}
一切正常,但出于可用性原因,我也想创建一个取消上传按钮。我尝试了以下操作:
此取消功能会停止所有当前的“POST”请求,但会重新从零开始。
$("#cancel").click(function(){
xhr.abort();
});
$( document ).ajaxStop(function() {
//do something after all ajax requests are finished or aborted
});
知道如何永久中止上传吗?
【问题讨论】: