xmlhtt prequest不会在功能上正常工作会在、正常、功能、工作

2023-09-11 00:40:17 作者:撒旦狂热族

有没有人知道为什么upload.onprogress不能正常工作,如果是在不同的功能?

code正常工作(进度条缓慢移动的):

  xhr.upload.onprogress =功能(E){
            如果(e.lengthComputable){
                progress.value =(e.loaded / e.total)* 100;
            }
        };
 

但如果我把它的功能,它不工作了:

  xhr.upload.onprogress =上传进度(事件);

功能上传进度(五){
    如果(e.lengthComputable){
        progress.value =(e.loaded / e.total)* 100;
    }
}
 

在第二code,进度条直接跳转到100%完成的文件上传,而不是后,在上载过程中很好地运动到100%

所以,我试着解决方案提供,它实际工作,如果我把里面的功能。请问有没有办法把它的功能之外?

 函数uploadFile(blobFile,文件名){
            ...
            ...

            //听每个上传上传进度。
            xhr.upload.onprogress =上传进度;

            //进度条计算,为什么它要在uploadFile功能..
            功能上传进度(五){
                如果(e.lengthComputable){
                    progress.value =(e.loaded / e.total)* 100;
                }
            }

            uploaders.push(XHR);
            xhr.send(FD);
        }

        //如果我把它的功能之外这是行不通的。反正是有这样做吗?
        功能上传进度(五){
             如果(e.lengthComputable){
                 progress.value =(e.loaded / e.total)* 100;
             }
        }
 
一举串接Shopify与WooCommerce,OneShip全新功能上线

解决方案

通过上传进度(事件); 调用该函数本身和返回值赋给 xhr.upload.onprogress ,而不是分配给它一个回调函数:

  xhr.upload.onprogress =上传进度;
 

is anyone know why upload.onprogress does not work correctly if it's on separate function?

Code work properly (the progress bar slowly moving):

        xhr.upload.onprogress = function(e) {
            if (e.lengthComputable) {
                progress.value = (e.loaded / e.total) * 100;
            }
        };  

but if I put it into function, it does not work anymore:

xhr.upload.onprogress = uploadProgress(event);

function uploadProgress(e) {
    if (e.lengthComputable) {
        progress.value = (e.loaded / e.total) * 100;
    }
}   

On the second code, the progress bar directly jump into 100% after file finished uploaded instead of, move nicely into 100% during the upload

so, I've tried the solution provided, it actually work, if I put the function inside. Is there no way to put it outside the function?

        function uploadFile(blobFile, fileName) {
            ...
            ...

            // Listen to the upload progress for each upload.
            xhr.upload.onprogress = uploadProgress;

            // Progress Bar Calculation why it has to be in uploadFile function..
            function uploadProgress(e) {
                if (e.lengthComputable) {
                    progress.value = (e.loaded / e.total) * 100;
                }
            }                            

            uploaders.push(xhr);
            xhr.send(fd);
        }  

        //it does not work if I put it outside the function. is there anyway to do this?  
        function uploadProgress(e) {
             if (e.lengthComputable) {
                 progress.value = (e.loaded / e.total) * 100;
             }
        }   

解决方案

With uploadProgress(event); you call the function itself and assign the return value to xhr.upload.onprogress instead of assigning it as a callback function:

xhr.upload.onprogress = uploadProgress;