浏览并从用户硬盘驱动器中选择文件在IE中未定义 [英] Browse and select files from user hard drive gives undefined in IE

查看:143
本文介绍了浏览并从用户硬盘驱动器中选择文件在IE中未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我使用我的输入按钮浏览用户计算机上的文件,它可以在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:


  1. 通过而不是 this.files into handleFiles() function:

  1. Pass this instead of this.files into handleFiles() 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: http://jsfiddle.net/phusick/fkY4k/

    See working example at jsFiddle: http://jsfiddle.net/phusick/fkY4k/

    这篇关于浏览并从用户硬盘驱动器中选择文件在IE中未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆