在上传图片之前,我想先显示图片。我尝试了以下代码,但它只能在火狐浏览器中工作。但我需要让它在IE中也可以工作。有没有人能为我提供最佳解决方案?下面是我的代码。我已经谷歌了一整天,但找不到解决方案。
enter code here
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) {
alert(path);
$('#' + 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]);
}
}
}
}
我的HTML是
<body><form method="post" >
<input type="file" class="file" onchange="loadname(this,'previewimg')" id="file" /><img src="about:blank" name="previewimg" id="previewimg" alt="" style="width:2in; height:2in"/>
</form>