浏览并选择用户硬盘驱动器中的文件在IE中未定义

Browse and select files from user hard drive gives undefined in IE

本文关键字:文件 IE 未定义 浏览 选择 用户 硬盘驱动器      更新时间:2023-09-26

当我使用输入按钮浏览用户计算机上的文件时,它可以在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属性并从它提供的路径解析文件名。

我的解决方案:

  1. this而不是this.files传递到handleFiles()函数中:

    <input type="file" onchange="handleFiles(this)">
    
  2. 像这样启动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 执行此操作。