简体   繁体   中英

Why does my redirect is not working?

my script calls my redirect function to early, so the last file of a batch upload is failing. I have been search the whole morning an tried different approaches, but without success.

function uploadFile(something, callback) {



    var fileInput = $('#fileList1');
    //var reader = new FileReader();
    console.log(fileInput);
    if ( trim( fileInput.val() ).length == 0 ) {
        return;
    }
    var fileList = []; 
    count = fileInput[0].files.length; 
    for(i = 0; i < count; i++){

        loadFile(fileInput[0].files[i]);

    }

    function loadFile(file){

        var reader = new FileReader(); 
        var fileName = getFileNameWithExtension( file);
        var file = file;
        while(reader.onprogress){
            console.log("reading");    
        }

        reader.onload = function(event) {
            var val = reader.result; 
            var text = val.split(',')[1];
            saveFile( fileName, text, parentId );
            if (!--count){
                redirect();                    
            }

        }

        reader.onerror = function(event) {
            console.error("File could not be read! Code " + reader.error.message);
        }
        reader.readAsDataURL(file);

    }

}
function redirect(){

    window.location.href = '/{!tempID}';
    return false;
}

Can someone give me a hint?

#

Hello, i have rewritten my methods a bit based on your suggestions. But the redirect is still called to early,...before all uploads are done.

 function uploadFile() { var fileInput = $('#fileList1'); console.log(fileInput); if ( trim( fileInput.val() ).length == 0 ) { return; } var countTwo = 0; count = fileInput[0].files.length; for(var i = 0; i < count; i++){ loadFile(fileInput[0].files[i], function(val){ console.log(val); if(val === 3){ setTimeout(()=>{redirect();}, 5000); } }); } function loadFile(file, callback){ var reader = new FileReader(); var fileName = getFileNameWithExtension( file); var file = file; while(reader.onprogress){ console.log("reading"); } reader.onload = function(event) { var val = reader.result; var text = val.split(',')[1]; saveFile( fileName, text, parentId ); console.log(" ct " + countTwo + " c " + count-1); countTwo++; if(!--count) callback(countTwo); } reader.onerror = function(event) { console.error("File could not be read! Code " + reader.error.message); } reader.readAsDataURL(file); } } 

Method 1: (Recommended)

Detect when your uploading ends. And in that callback, call redirect.

Method 2:

// define your TIMEOUT first
setTimeout(()=>{redirect();}, TIMEOUT);
reader.onload = function(event) {
            var val = reader.result; 
            var text = val.split(',')[1];
            saveFile( fileName, text, parentId );
            if (!--count){
                setTimeout(()=>{redirect();}, 0);                    
            }

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM