English 中文(简体)
JQuery - 获取所有文件在输入=文件内Name
原标题:JQuery - get all fileNames inside input= file

我有 , 我想在这个输入中获取所有文件名称。 我见过一些例子, 但只有第一个文件的名称 。

$ ( #basicUploadFile ).live ( change , function () {
    alert($ ( #basicUploadFile ).val());
});

我该怎么做?

最佳回答
var files = $( #basicUploadFile ).prop("files")

files 将是一个文件列表对象。

var names = $.map(files, function(val) { return val.name; });

现在 names 是一组字符串( 文件名)

FileAPI reference
files property reference

问题回答

jsFiddle Demo


您仍然可以将文件作为 FileList 收藏访问,而无需过度使用 jQuery 。 我创建了一个快速的 href="http://jsfiddle.net/r64uV/1/" rel="noreferr" >jsFiddle , 演示如何使用 FileList File 对象从输入中获取信息。 这里有一个片断 :

$( #basicUploadFile ).live( change , function ()
{
    for (var i = 0; i < this.files.length; i++)
    {
        alert(this.files[i].name);
        alert(this.files.item(i).name); // alternatively
    }
});

我用这个在控制台显示所有文件名称 :

var input_file = $("#input_file");
input_file.on("change", function () {
     var files = input_file.prop("files")
     var names = $.map(files, function (val) { return val.name; });
     $.each(names, function (i, name) {
          console.log(name);
     });
});
<input name="selectAttachment" id="selectAttachment" type="file" multiple="multiple">
<button class="btn btn-default" onclick="uploadAttachment()" type="submit">Upload</button>


function uploadAttachment() {
                debugger;
                var files = $( #selectAttachment ).prop( files );
                var names = $.map(files, function (val) { return val.name; });
            }

您可以扩展 < strong > File 对象 (例如 File. prototype.toJSON ) 的原型,您也可以访问 的文件列表 :

<input id="myFile" type="file">
 var file = document.getElementById( fileItem ).files[0];

有关此文档的更多信息,请查看 :

https:// developmenter.mozilla.org/en-US/docs/Web/API/FileList#Using_the_file_list" rel=“不跟随 Noreferrer'>https://developter.mozilla.org/en-US/docs/Web/API/FileList#Using_the_file_list

check this simple example:

File.prototype.toJSON = function() {
	return {
		 lastModified      : this.lastModified,
		 lastModifiedDate  : this.lastModifiedDate,
		 name              : this.name,
		 size              : this.size,
		 type              : this.type 
	};
}

function getFiles() {
    var files = document.getElementById( myFiles ).files;
    document.getElementById( result ).innerHTML =  <h1>result</h1> 
      + JSON.stringify(files);
}
<input id="myFiles" type="file" multiple onchange="getFiles()" />
<pre id= result ></pre>

祝你好运!





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签