问题描述
当我使用我的输入按钮浏览用户计算机上的文件,它可以在FF,IE9和Chrome上工作。但是,当我将文件传递给IE9中的JS函数时,我会在FF和Chrome中完美运行,而且它的功能非常明确。
when I use my input button to browse for the file on the user computer it works on FF, IE9 and Chrome. But when I am passing the files to the JS function in IE9 I get undefined, while it works perfectly in FF and Chrome.
<form id="uploadForm" style='display:none;padding:1px;' method="post" enctype="multipart/form-data">
<input type="file" name="data" id="inFile" size="15" style="display:none" onchange="handleFiles(this.files)"/>
function handleFiles(files){
//doing something with the files
}
//In IE files is undefined
我也尝试使用
dojo.connect(dojo.byId("uploadForm").data, "onchange", function(evt){
handleFiles(this.files);
});
<form id="uploadForm" method="post" enctype="multipart/form-data">
<input type="file" name="data" id="inFile" size="15" style="display:none"/>
This.files再次未定义
This.files comes undefined again
谢谢
推荐答案
IE9不支持多个文件上传,它没有文件
属性。您将不得不依赖值
属性并从其提供的路径中解析文件名。
IE9 does not support multiple files upload and it does not have files
property. You will have to rely on value
property and parse a filename from the path it provides.
我的解决方案: / p>
My solution:
-
通过
此
而不是this.files
intohandleFiles()
function:
Pass
this
instead ofthis.files
intohandleFiles()
function:
<input type="file" onchange="handleFiles(this)">
启动您的 handleFiles()
函数如下:
function handleFiles(input){
var files = input.files;
if (!files) {
// workaround for IE9
files = [];
files.push({
name: input.value.substring(input.value.lastIndexOf("\\")+1),
size: 0, // it's not possible to get file size w/o flash or so
type: input.value.substring(input.value.lastIndexOf(".")+1)
});
}
// do whatever you need to with the `files` variable
console.log(files);
}
jsFiddle:
See working example at jsFiddle: http://jsfiddle.net/phusick/fkY4k/
这篇关于浏览并从用户硬盘驱动器中选择文件在IE中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!