首页 > Web开发 > 详细

html5读取本地文件,图片预览

时间:2015-08-20 18:40:36      阅读:288      评论:0      收藏:0      [点我收藏+]

案例1,实现本地图片预览,URL.createObjectURL(file)

URL.createObjectURL()创建一个新的对象URL,该对象URL可以代表某一个指定的File对象或Blob对象.

html代码

<input type="file" id="changeMore">
<div id="ImgCon"></div>

js代码

 1 function changeM(){
 2         var inputJ = $("#changeMore"),
 3             input  = inputJ[0],
 4             Con    = $("#ImgCon");
 5 
 6         inputJ.change(function(e){
 7             var file     = e.target.files[0],//拿到原始对象
 8                 thisType = file.type,//获取到表面的名称,可判断文件类型
 9                 thisSize = file.size,//文件的大小
10                 thisSrc  = URL.createObjectURL(file),//当前对象的地址
11                 img      = $(‘<img>‘).attr(‘src‘,thisSrc);//创建img对象
12 
13                 //文件加载成功以后,渲染到页面
14                 img.load(function(){
15                     Con.append(img);
16                     URL.revokeObjectURL(thisSrc);//释放内存
17                 });
18         });    
19     }//
20     changeM();

 

html5读取本地文件,图片预览

原文:http://www.cnblogs.com/xxyy1122/p/4745956.html

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