首页 > 其他 > 详细

img标签显示本地文件

时间:2016-07-15 00:24:05      阅读:192      评论:0      收藏:0      [点我收藏+]

html:

<img src="__IMG__/male.png" id="imgfpic1" style="height: 100%; width: auto; max-width: 100%;">
<input type="file" id="pic" name="pic" onchange="setpreimg();" style="position:absolute; top: 0px; left: 0px; bottom: 0px; width: 100%; height: 100%; z-index: 999; opacity: 0;"/>

js:
//使用html5的FileReader属性
if(window.FileReader){
var oFReader = new window.FileReader(),
rFilter = /^(?:image\/bmp|image\/cis\-cod|image\/gif|image\/ief|image\/jpeg|image\/jpeg|image\/jpeg|image\/pipeg|image\/png|image\/svg\+xml|image\/tiff|image\/x\-cmu\-raster|image\/x\-cmx|image\/x\-icon|image\/x\-portable\-anymap|image\/x\-portable\-bitmap|image\/x\-portable\-graymap|image\/x\-portable\-pixmap|image\/x\-rgb|image\/x\-xbitmap|image\/x\-xpixmap|image\/x\-xwindowdump)$/i;

oFReader.onload = function (e) {
$("#imgfpic1").attr(‘src‘, e.target.result);
};

var aFiles = document.getElementById("pic").files;
if (aFiles.length === 0) { return; }
if (!rFilter.test(aFiles[0].type)) { alert("You must select a valid image file!"); return; }
oFReader.readAsDataURL(aFiles[0]);
}








img标签显示本地文件

原文:http://www.cnblogs.com/w10234/p/5671997.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!