Javascript Jquery input.files 等效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13747892/
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 input.files equivalent
提问by Mansfield
I have a simple html form with a single file upload input. (jsfiddle)
我有一个带有单个文件上传输入的简单 html 表单。( jsfiddle )
In the past, I have accessed the file selected by the user using input.files, however I am at a loss as to how to do this with JQuery;
过去,我使用 访问了用户选择的文件input.files,但是我不知道如何使用 JQuery 执行此操作;
Code:
代码:
$(function () {
$("#cmdSubmit").bind("click", function () {
var file = document.getElementById("fileInput").files[0];
alert(file); //A
file = $("#fileInput").val();
alert(file); //B
file = $("#fileInput").files[0];
alert(file); //C
});
});?
Option Agive me what I expect, a file object. However, option Bsimply gives me the name of the uploaded file, and (as far as I can tell) not the file itself.
选项A给了我我所期望的,一个文件对象。但是,选项B只是给了我上传文件的名称,而不是(据我所知)文件本身。
Option Cshows that filesis undefined.
选项C显示files未定义。
What is the Jquery equivalent of input.files?
什么是 Jquery 等价物input.files?
Note: I have no objection to using native javascript; but given that I am using JQuery throughout the rest of this project I'd prefer to use it here as well if possible.
注意:我不反对使用原生 javascript;但考虑到我在这个项目的其余部分都使用 JQuery,如果可能的话,我更愿意在这里使用它。
回答by Stefan
回答by Quentin
jQuery doesn't provide a wrapper for the Files API. So there isn't a jQuery style way to do this, at least not built into the jQuery core.
jQuery 没有为 Files API 提供包装器。所以没有一种 jQuery 风格的方式来做到这一点,至少没有内置到 jQuery 核心中。
Your options:
您的选择:
- stick with option A
- wait for jQuery to include such a wrapper (they might not)
- find an extension written by someone else (if one exists)
- write an extension yourself
- 坚持选项A
- 等待 jQuery 包含这样的包装器(他们可能不会)
- 找到由其他人编写的扩展程序(如果存在)
- 自己写一个扩展
回答by Sridhar Narasimhan
Try the below code
试试下面的代码
var file = $("#fileInput").get(0).files[0];
alert(file);
Thanks
谢谢
回答by arhea
$(function () {
$("#cmdSubmit").bind("click", function () {
alert(this.files);
});
});?

