显示选择在Chrome和mozilla中上传的图像
问题描述:
在我的一个Web应用程序中,必须使用javascript显示选择上传的图像,然后再将其上传到服务器.
In one of my web application I have to show the image which selected for uploading before it uploaded to the server , using javascript .
我有这段代码...在Mozilla中,它运行得很好并且很好.但不是在Safari或Chrome浏览器中..请帮助
I had this code ... It is working pretty well and fine in Mozilla . But not in Safari or Chrome .. Please help
// 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);
}
}
答
此代码在Chrome浏览器中正常工作
This code is working fine in chrome
它以chrome& FF.
Its shows the selected image in chrome & FF.
更新
您可以使用以下代码检查文件读取器的功能
you can check file reader capability with following code
// 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.');
}
在我的野生动物园中,它失败了,因为它不完全支持文件API.
In my safari it failed because of it is not supporting file API fully.