这有点复杂:我有一个带有文件输入字段的表单和下面的上传按钮。通过单击该上传按钮,将触发一个函数,该函数执行ajax调用以检查上载目录中是否已存在具有相同名称的文件。如果文件不存在,则会触发" start_upload" -function:
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);
}
一切正常但由于可用性原因我也想创建一个取消上传按钮。我试过以下:
此取消功能会停止所有当前&#34; POST&#34; - 请求,但会从零开始。
$("#cancel").click(function(){
xhr.abort();
});
$( document ).ajaxStop(function() {
//do something after all ajax requests are finished or aborted
});
知道如何永久中止上传内容吗?
答案 0 :(得分:1)
您可以使用标记值,如下所示:
int flag=0;
function start_upload(){
while(start < SIZE && flag==0) { //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;
}
}
您的取消按钮将是:
$("#cancel").click(function(){
flag=1;
});
希望它对你有用。