如何使用javascript或jquery从html 5中的输入类型文件中获取文件名

Khu*_*jaz 2 javascript php jquery

下面是我使用HTML 5多文件上传的简单代码.我需要的是获取所有文件名,而不是下面的路径是简单的JavaScript代码

  function getFileNames()
        {
            var path = $('#files').val();
            console.log(' path = ' + path);
        }
Run Code Online (Sandbox Code Playgroud)

和HTML表单是一样的

<form action='server.php' method='post' enctype='multipart/form-data' target="iframe">
                <input id="files" name='files[]' type='file' multiple>
                <input type='button' onclick="getFileNames()">
</form>
Run Code Online (Sandbox Code Playgroud)

当我按下按钮时,控制台输出为

path = Chrysanthemum.jpg

这是该文件的第一个名称,我想要其余的名称,任何建议,评论表示赞赏.谢谢.

dku*_*mar 6

也所以我在这里有很多的研究后的溶液中的情况下,input type file该值存储在array作为files与关键name.

var files = $('input#files')[0].files;
var names = "";
$.each(files,function(i, file){
    names += file.name + " ";
});
alert(names);
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/raj_er04/nze2B/1/

纯粹的javascript

function getFileNames(){
    var files = document.getElementById("files").files;
    var names = "";
    for(var i = 0; i < files.length; i++)
        names += files[i].name + " ";
    alert(names);
}
Run Code Online (Sandbox Code Playgroud)

小提琴:http://jsfiddle.net/raj_er04/nze2B/2/