且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2022-03-08 08:40:09

好吧所以我在这里经过大量研究后的解决方案,如果输入类型文件该值存储在数组中,为 files ,其中包含键名称

well so here i am with the solution after lot of research, in case of input type file the value is stored in array as files with key name.

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

小提琴: 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);
}

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