如何通过一个上传按钮上传多个文件

时间:2016-06-08 07:08:16

标签: javascript php html css

当我执行它时,我的所有文件都没有上传,只是其中一个。

的JavaScript

import {isString, isArray} from "@angular/common/src/facade/lang";

HTML

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

2 个答案:

答案 0 :(得分:1)

你应该做循环

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

答案 1 :(得分:0)

试试这会帮到你

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