2016-06-08 15 views
5

Durante l'esecuzione non vengono caricati tutti i miei file, ma solo uno di essi.Come caricare più file tramite un pulsante di caricamento

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> 
+1

Possibile duplicato di [File multipli selezionare e caricare] (http://stackoverflow.com/questions/8669579/multiple-files-select-and-upload) – billynoah

risposta

1

si dovrebbe fare ciclo

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

provare questo vi aiuterà a

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) { 

     if(!e.target.files) return; 

     var files = e.target.files; 
     for(var i=0; i < files.length; i++) { 
      var f = files[i]; 
      document.getElementById("uploading").innerHTML="<h3>File uploaded: "+f; 
     } 
    }