gpt4 book ai didi

html - 如何在各种浏览器上传前预览图片

转载 作者:太空狗 更新时间:2023-10-29 14:54:32 26 4
gpt4 key购买 nike

我想在图片上传前显示预览。我找到了适用于 ie6 和 firefox 的部分解决方案,但尚未在 ie7 或 ie8 中对其进行测试。但我想要一个适用于 safari、ie7 和 ie8 的解决方案。下面是结合ie6和firefox方案得到的方案:

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 个浏览器中工作,我会很高兴。如果没有,那么唯一的选择是拥有两个表单以及另一个表单的图像上传部分吗?

最佳答案

你可以使用打击功能。在 IE7+ 和 Firefox 和 chrome 上测试

function loadname(img, previewName){  

var isIE = (navigator.appName=="Microsoft Internet Explorer");
var path = img.value;
var ext = path.substring(path.lastIndexOf('.') + 1).toLowerCase();

if(ext == "gif" || ext == "jpeg" || ext == "jpg" || ext == "png" )
{
if(isIE) {
$('#'+ previewName).attr('src', path);
}else{
if (img.files[0])
{
var reader = new FileReader();
reader.onload = function (e) {
$('#'+ previewName).attr('src', e.target.result);
}
reader.readAsDataURL(img.files[0]);
}
}

}else{
incorrect file type
}
}

<input type="file" name="image" onchange("loadname(this,'previewimg')") >
<img src="about:blank" name="previewimg" id="previewimg" alt="">

关于html - 如何在各种浏览器上传前预览图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1887519/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com