我想在图像上传之前显示图像预览。我发现了一个适用于ie6和firefox的部分解决方案,并且尚未在ie7或ie8中对其进行测试。但我想要一个在safari中工作的解决方案,即ie7和ie8。这是通过将IE6和火狐的解决方案所获得的解决方案:如何在上传之前在各种浏览器中预览图像
function preview(what) {
if(jQuery.browser.msie) {
document.getElementById("preview-photo").src=what.value;
return;
}
else if(jQuery.browser.safari) {
document.getElementById("preview-photo").src=what.value;
return;
}
document.getElementById("preview-photo").src=what.files[0].getAsDataURL();
// alert(jQuery("#preview-photo").height());
// alert(jQuery("#preview-photo").width());
var h = jQuery("#preview-photo").height();
var w = jQuery("#preview-photo").width();//assuming width is 68, and height is floating
if ((h > 68) || (w > 68)){
if (h > w){
jQuery("#preview-photo").css("height", "68px");
jQuery("#preview-photo").css("width", "auto");
}else {
jQuery("#preview-photo").css("width", "68px");
jQuery("#preview-photo").css("height", "auto");
}
}
}
的getAsDataURL()部分工作在Firefox,而“SRC = what.value”部分中的IE6的作品,但你会在Safari工作,并且“src = what.value”在ie7和ie8中也有效?如果没有,是否有一些解决方案也可以在那里工作?如果我可以在5个或6个浏览器中进行图像预览,我会很高兴。如果它不是,那么是唯一的选择有两种形式与另一种形式的图像上传部分?
事实并非如此。新的HTML5 File API完全适用于此目的。它完全保护文件路径,你必须通过FileReader读取所有数据。 – 2011-04-12 18:33:57
注意这个答案是3岁。 – Ash 2012-12-14 18:57:21