Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload multiple files through one upload button

When I am executing it not all of my files are uploaded but just one of them.

JavaScript

function upload() {
    document.getElementById("uploading").innerHTML="uploading....";
    var myfile=document.getElementById("fileinput").files[0];

    //alert(myfile.size);
    var r = new FileReader();

    r.onload = function(e) { 
        var contents = e.target.result;

        parseContents(contents);
        //document.getElementById("cont").innerHTML=fileContent;

        document.getElementById("uploading").innerHTML="<h3>File uploaded: "+myfile.name;
    }

    r.readAsText(myfile);
}

HTML

<body onload="initialize()">
    <div id="container1"><h>MY TRANSIT PLANNER</h></div>
    <h3 style="text-decoration:underline;">choose a file for input:</h3>
    <input type="file" id="fileinput" multiple="multiple"onchange="upload()"/>
    <br>
    <div style="color: black" id="uploading"></div>
    <script src="https://maps.googleapis.com/maps/api/js?"async defer></script>
    <input type="button" id="btn-sgtd" type="text" value="SAVE GTD" onclick="writetofile()"/>
    <h3 style="text-decoration:underline;">Choose files to Segment: </h3>
    <form action="files.php" method="POST" enctype="multipart/form-data">
        <input type="file" name="my_file[]" multiple="multiple"><br>
        <br>
        <input type="submit" value="SEGMENT" class="button"><br>
    </form>
    <div id="map"></div>
</body>
like image 467
Rajan Avatar asked Nov 08 '22 13:11

Rajan


1 Answers

you should do loop

   function upload() {


    for (var i = 0; i < document.getElementById("fileinput").files.length; i++) 
        {                           
                document.getElementById("uploading").innerHTML="uploading....";
                var myfile=document.getElementById("fileinput").files[i];
                //alert(myfile.size);
                var r = new FileReader();
                r.onload = function(e) 
                { 
                    var contents = e.target.result;

                    parseContents(contents);
                    //document.getElementById("cont").innerHTML=fileContent;

                    document.getElementById("uploading").innerHTML="<h3>File uploaded: "+myfile.name;
                }

                r.readAsText(myfile);
        }
}
like image 102
emirsnck Avatar answered Nov 14 '22 21:11

emirsnck