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

2023-11-20

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

  1. 将文件分成大约一兆字节的较小块。
  2. 上传每个块,并等待其完成后再开始上传下一个块。
  3. 对于每个块,获取成功或失败报告。
  4. 重新上传失败的块。
  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);
            }

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


您不会因任何块上传失败而执行任何操作。

$.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.
                }
            })              
        };
}

EDIT

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

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
   }
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐