Home » Javascript » Read Multiple Files With JavaScript and Wait for Result

Read Multiple Files With JavaScript and Wait for Result

Posted by: admin August 19, 2018 Leave a comment


I would like to start by saying that (coming from c++ and python) I am totally new to JS and so I welcome any wise suggestions regarding my code.

I wish to read a number of files using the HTML5 file API, then open them with JS, perform some manipulation and download the results zipped. My problem is that reading the files seems to be an asynchronous operation, and I don’t really see an elegant way to wait for them all to finish and then zip the results.

One possible solution is presented here:
but I am wondering if one can do better than using a global flag.

I also have a problem with retrieving the result from the async function as I have no idea how to get back new_file_list in changeCharsInFiles.

Thank you!

Code example:

    <!DOCTYPE html>
    <html lang="en">
        <meta charset="utf-8">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
    <body >
        <div class="container">
            <div class="jumbotron">
                <h3>Add Files Here</h3>
                <input type="file" id="the-file-field" multiple>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.5/jszip.min.js"></script>
        <script src="http://cdn.jsdelivr.net/g/filesaver.js"></script>
         ########### SEE JS BELOW #####################


if (window.File && window.FileReader && window.FileList && window.Blob) {

                function zipMyFilesAndSave(file_list){

                    var zip = new JSZip();
                    for (var i = 0; i<file_list.length; i+=1)
                        zip.file(file_list[i].name, file_list[i]  );
                    function (blob) {                                      
                        saveAs(blob, "hello.zip");                         
                    function (err) {

                function changeCharsInFiles(file_list){
                    var new_file_list = [];

                    for (var i = 0; i<file_list.length; i+=1)
                        var file = file_list[i]

                        var reader = new FileReader();
                        reader.onload = function() {
                            var txt  = reader.result;
                            console.log("txt: ",txt)

                            var new_txt = ""
                            var allTextLines = txt.split(/\r\n|\n/);

                            for (var j = 0; j<allTextLines.length; j+=1)
                                var res = allTextLines[j].replace("a", "A");
                                res = res.replace("b", "B");
                                res = res.replace("c", "C");
                                res = res.replace("d", "D");

                                new_txt += res + "\n"

                            console.log("new_txt: ", new_txt)
                            var new_file = new Blob([new_txt], {type: "text/plain"});

                            new_file_list.push(new_file); //<---------------------------how do I get this back?


                    return new_file_list;

                $( "#the-file-field" ).change(function() {
                    console.log("files have been chosen")
                    var file_list = this.files
                    file_list = changeCharsInFiles(file_list)

            } else {
                alert('The File APIs are not fully supported in this browser.');