微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何在小块中上传带有ajax的文件并检查失败,重新上传失败的部分.

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

>将文件划分为大约一兆字节的较小块.
>上传每个块,并在开始上传一个块之前等待它完成.
>为每个块获得成功或失败报告.
>重新上传失败的块.
>以百分比取得进展.

这是一些粗略的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, onl oad 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 onl oad 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.
                }
            })              
        };
}

编辑

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

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐