6

我有一个:

<img id="uploadedimage" alt="uploaded image" src="" width="250px" height="250px"/>

一旦用户使用此 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();
            }
        });

当然,这段代码在除撒旦浏览器 Internet Explorer 之外的所有其他浏览器中都运行良好。我收到此错误:

Line: 108
Character: 13
Code: 0
Error Message: Unable to get value of the property '0': object is null or undefined

有谁知道是什么原因造成的,因为它在 FFX 和 Chrome 中运行良好。

谢谢

4

1 回答 1

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 );
}
于 2012-07-30T14:25:24.137 回答