IE Java脚本错误无法获取属性"0"的值:

Fun*_*nky 6 jquery internet-explorer file-upload

我有一个:

<img id="uploadedimage" alt="uploaded image" src="" width="250px" height="250px"/>
Run Code Online (Sandbox Code Playgroud)

并且在用户使用此JQuery代码选择其图像后,使用div显示图像:

$('#BusinessImage').change(function (ev) {

            var f = ev.target.files[0];
            var fr = new FileReader();
            var IsImage = false;

            // check the file is an image
            if (f.type.match('image.*')) {
                IsImage = true;
            }

            fr.onload = function (ev2) {
                if (IsImage) {
                    $('#uploadedimage').attr('src', ev2.target.result);  
                }
            };

            if (IsImage) {
                fr.readAsDataURL(f);
                ValidFileUpload();
            }
            else {
                InvalidFileUpload();
            }
        });
Run Code Online (Sandbox Code Playgroud)

当然,除了Satans浏览器,Internet Explorer之外,这个代码在其他所有浏览器中都很有用.我收到此错误:

Line: 108
Character: 13
Code: 0
Error Message: Unable to get value of the property '0': object is null or undefined
Run Code Online (Sandbox Code Playgroud)

有没有人知道是什么导致了这一点,因为它在FFX和Chrome中运行良好.

谢谢

Dan*_*nda 8

".files"仅适用于那些支持HTML5的浏览器.

IE10支持文件,但对于IE9和早期版本,您必须使用其他方式获取路径:

要检查文件是否受支持:

if( ev.target.files ){
  //supported
  console.log( ev.target.files[0] );
}else{
  //.files not supported
  console.log( ev.target.value );
}
Run Code Online (Sandbox Code Playgroud)