JQuery - 文件属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8436315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery - File attributes
提问by JimmyJammed
Trying to access file attributes from an input field after a file is selected. Tried this but get the error 'file not defined'
在选择文件后尝试从输入字段访问文件属性。试过这个但得到错误“文件未定义”
var file = $("#uploadedfile").prop("files")[0];
var fileName = file.fileName;
var fileSize = file.fileSize;
alert("Uploading: "+fileName+" @ "+fileSize+"bytes");
回答by adeneo
If #uploadedfile
is an input with type "file" :
如果#uploadedfile
是类型为 "file" 的输入:
var file = $("#uploadedfile")[0].files[0];
var fileName = file.name;
var fileSize = file.size;
alert("Uploading: "+fileName+" @ "+fileSize+"bytes");
Normally this would fire on the change event, like so:
通常这会在更改事件上触发,如下所示:
$("#uploadedfile").on("change", function(){
var file = this.files[0],
fileName = file.name,
fileSize = file.size;
alert("Uploading: "+fileName+" @ "+fileSize+"bytes");
CustomFileHandlingFunction(file);
});
回答by Srinivasan.S
<form id = "uploadForm" name = "uploadForm" enctype="multipart/form-data">
<label for="uploadFile">Upload Your File</label>
<input type="file" name="uploadFile" id="uploadFile">
</form>
<script>
$('#uploadFile').change(function(){
var fileName = this.files[0].name;
var fileSize = this.files[0].size;
var fileType = this.files[0].type;
alert('FileName : ' + fileName + '\nFileSize : ' + fileSize + ' bytes');
});
</script>
Note:To get the uploading file name means then use jquery val() method.
注意:要获取上传文件名意味着使用 jquery val() 方法。
For Ex:
例如:
var fileName = $('#uploadFile').val();
I checked this above code before post, it works perfectly.!
我在发布之前检查了上面的代码,它完美无缺。!
回答by David says reinstate Monica
To get the filenames, use:
要获取文件名,请使用:
var files = document.getElementById('inputElementID').files;
Using jQuery (since you already are) you can adapt this to the following:
使用 jQuery(因为您已经是),您可以将其调整为以下内容:
$('input[type="file"][multiple]').change(
function(e){
var files = this.files;
for (i=0;i<files.length;i++){
console.log(files[i].fileName + ' (' + files[i].fileSize + ').');
}
return false;
});
回答by Suraj Poddar
Just try
你试一试
var file = $("#uploadedfile").prop("files")[0];
var fileName = file.name;
var fileSize = file.size;
alert("Uploading: "+fileName+" @ "+fileSize+"bytes");
It worked for me
它对我有用
回答by Fredrik Blomqvist
The input.files
attribute is an HTML5 feature. That's why some browsers din't return anything.
Simply add a fallback to the plain old input.value
(string) if files
doesn't exist.
该input.files
属性是 HTML5 功能。这就是为什么有些浏览器什么都不返回的原因。input.value
如果files
不存在,只需向普通旧(字符串)添加后备。
reference: http://www.w3.org/TR/2012/WD-html5-20121025/common-input-element-apis.html#dom-input-files
参考:http: //www.w3.org/TR/2012/WD-html5-20121025/common-input-element-apis.html#dom-input-files