浏览并从用户硬盘中选择文件在IE中给出undefined

set*_*lio 2 html javascript dojo

当我使用我的输入按钮浏览用户计算机上的文件时,它适用于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
Run Code Online (Sandbox Code Playgroud)

我也试过用

    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"/>
Run Code Online (Sandbox Code Playgroud)

This.files再次未定义

谢谢

phu*_*ick 6

IE9不支持多个文件上传,也没有files属性.您将不得不依赖value属性并从其提供的路径解析文件名.

我的解决方案

  1. 通过this而不是this.fileshandleFiles()功能:

    <input type="file" onchange="handleFiles(this)">
    
    Run Code Online (Sandbox Code Playgroud)
  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);
    }
    
    Run Code Online (Sandbox Code Playgroud)

请参阅jsFiddle的工作示例:http://jsfiddle.net/phusick/fkY4k/