2012-02-28 59 views
0

在我的一个Web应用程序中,我必须使用javascript显示上传到服务器之前上传的图像。选择要在Chrome和mozilla中上传的显示图像

我有这个代码...它在Mozilla中工作得很好。但不是在Safari或Chrome。请帮助

// Handle file while select a new file 
$('#file').change(function(){ 
     $('#img_size').val((this.files[0].size)/1000000); 
        handleFiles(this.files); 
}); 


// handle files 

function handleFiles(files) { 





    for (var i = 0; i < files.length; i++) { 
    var file = files[i]; 

    var imageType = /image.*/; 

    if (!file.type.match(imageType)) { 
     continue; 
    } 


    var img=document.getElementById('fake_img'); 
    img.src = file; 
    img.onload = function() { 



    }; 



    var reader = new FileReader(); 
    reader.onload = (function(aImg) { 
     return function(e) { 
      aImg.src = e.target.result; 
     }; 
    })(img); 
    reader.readAsDataURL(file); 
} 

} 

回答

2

此代码工作正常铬

http://jsfiddle.net/PrNWY/13/

它显示了镀铬& FF所选择的图像。

更新

你可以检查文件的读写能力与下面的代码

// Check for the various File API support. 
if (window.File && window.FileReader && window.FileList && window.Blob) { 
    // Great success! All the File APIs are supported. 
    alert('The File APIs are fully supported in this browser.'); 
} else { 
    alert('The File APIs are not fully supported in this browser.'); 
} 

在我的Safari浏览器失败,因为它并不完全支持文件API。