gpt4 book ai didi

javascript - 如何使用 ajax 分块上传文件并检查是否失败,重新上传失败的部分。

转载 作者:可可西里 更新时间:2023-11-01 14:01:17 25 4
gpt4 key购买 nike

我有一个用户上传的文件,我想实现以下目标。

  1. 将文件分成大约 1 兆字节的较小块。
  2. 上传每个 block ,并等待它完成后再开始上传下一个 block 。
  3. 对于每个 block 都获得成功或失败报告。
  4. 重新上传失败的 block 。
  5. 以百分比形式取得进步。

这是一些粗略的 JavaScript。我真的迷路了。在线获取了一些代码并尝试对其进行修改。

$.chunky = function(file, name){        
var loaded = 0;
var step = 1048576//1024*1024;
var total = file.size;
var start = 0;
var reader = new FileReader();

reader.onload = function(e){

var d = {file:reader.result}
$.ajax({
url:"../record/c/index.php",
type:"POST",
data:d}).done(function(r){
$('.record_reply_g').html(r);

loaded += step;
$('.upload_rpogress').html((loaded/total) * 100);

if(loaded <= total){
blob = file.slice(loaded,loaded+step);
reader.readAsBinaryString(blob);
} else {
loaded = total;
}
})
};

var blob = file.slice(start,step);
reader.readAsBinaryString(blob);
}

我怎样才能实现上述目标。如果有可行的解决方案,请解释发生了什么。

最佳答案

您没有为任何 block 上传失败做任何事情。

$.chunky = function(file, name){        
var loaded = 0;
var step = 1048576//1024*1024; size of one chunk
var total = file.size; // total size of file
var start = 0; // starting position
var reader = new FileReader();
var blob = file.slice(start,step); //a single chunk in starting of step size
reader.readAsBinaryString(blob); // reading that chunk. when it read it, onload will be invoked

reader.onload = function(e){
var d = {file:reader.result}
$.ajax({
url:"../record/c/index.php",
type:"POST",
data:d // d is the chunk got by readAsBinaryString(...)
}).done(function(r){ // if 'd' is uploaded successfully then ->
$('.record_reply_g').html(r); //updating status in html view

loaded += step; //increasing loaded which is being used as start position for next chunk
$('.upload_rpogress').html((loaded/total) * 100);

if(loaded <= total){ // if file is not completely uploaded
blob = file.slice(loaded,loaded+step); // getting next chunk
reader.readAsBinaryString(blob); //reading it through file reader which will call onload again. So it will happen recursively until file is completely uploaded.
} else { // if file is uploaded completely
loaded = total; // just changed loaded which could be used to show status.
}
})
};
}

编辑

要再次上传失败的 block ,您可以执行以下操作:

var totalFailures = 0;
reader.onload = function(e) {
....
}).done(function(r){
totalFailures = 0;
....
}).fail(function(r){ // if upload failed
if((totalFailure++) < 3) { // atleast try 3 times to upload file even on failure
reader.readAsBinaryString(blob);
} else { // if file upload is failed 4th time
// show message to user that file uploading process is failed
}
});

关于javascript - 如何使用 ajax 分块上传文件并检查是否失败,重新上传失败的部分。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33537769/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com