当我使用输入按钮浏览用户计算机上的文件时,它可以在FF,IE9和Chrome上运行。但是当我将文件传递给IE9中的JS函数时,我没有得到定义,而它在FF和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"/>
这个文件再次未定义
谢谢
IE9不支持多个文件上传,也没有files
属性。您将不得不依赖value
属性并从它提供的路径解析文件名。
我的解决方案:
-
将
this
而不是this.files
传递到handleFiles()
函数中:<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的工作示例:http://jsfiddle.net/phusick/fkY4k/
显然,IE中没有定义文件。请参阅此处了解如何使用 IE 执行此操作。