jQuery-文件属性 [英] JQuery - File attributes

查看:89
本文介绍了jQuery-文件属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在选择文件后尝试从输入字段访问文件属性. 尝试了此操作,但收到错误文件未定义"

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");

推荐答案

如果#uploadedfile是类型为"file"的输入:

If #uploadedfile is an input with type "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);
});

凯文(Kevin)对文件属性的评论正确无误!

Kevin is right in his comment about the files property, edited!

再次正确的语法不是

filename = this.files[0].filename;

但是

filename = this.files[0].name;
filesize = this.files[0].size;

添加可以在浏览器中正常运行的小提琴: http://jsfiddle.net/eq3Qv/

Adding a Fiddle that should work cross browser : http://jsfiddle.net/eq3Qv/

这篇关于jQuery-文件属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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