87,921
社区成员
发帖
与我相关
我的任务
分享
<td width="18%" id="">
<input type="file" style="width:200px;" id='PictureFile' name='PictureFile' onchange="showPicture('PictureFile','imgPre')"/>
<img src="" id="imgPre" width="100px" height="100px" style="display: block"/>
</td>
//将本地图片 显示到浏览器上
function showPicture(sourceId, targetId) {
var url = getFileUrl(sourceId);
var imgPre = document.getElementById(targetId);
imgPre.setAttribute("src", url);
}
//从 file 域获取 本地图片 url
function getFileUrl(sourceId) {
var url='';
if (navigator.userAgent.indexOf("MSIE") >= 1) { // IE
url = document.getElementById(sourceId).value;
} else if (navigator.userAgent.indexOf("Firefox") > 0) { // Firefox
url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
} else if (navigator.userAgent.indexOf("Chrome") > 0) { // Chrome
url = window.URL.createObjectURL(document.getElementById(sourceId).files.item(0));
}
return url;
}