如何使用javascript或jquery从html 5中的输入类型文件中获取文件名 [英] how to get file names from input type file in html 5 using javascript or jquery

查看:65
本文介绍了如何使用javascript或jquery从html 5中的输入类型文件中获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

below is my simple code using HTML 5 multiple file upload. All i need is to grab all the file names not the paths below is simple javascript code

  function getFileNames()
        {
            var path = $('#files').val();
            console.log(' path = ' + path);
        }

和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>

按下按钮时控制台输出为

when i press the button the console output is as

path = Chrysanthemum.jpg

path = Chrysanthemum.jpg

这是该文件的第一个名称,我想要剩下的名字,任何建议,评论都表示赞赏。
谢谢。

which is the first name of the file and i want the remaining names , any suggestions , comments are appreciated. thanks.

推荐答案

好吧所以我在这里经过大量研究后的解决方案,如果输入类型文件该值存储在数组中,为 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/

这篇关于如何使用javascript或jquery从html 5中的输入类型文件中获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆