Rajan Rajan - 6 months ago 36
CSS Question

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>

Answer

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