js读取本地文件
来源:互联网 发布:超牛手机数据恢复软件 编辑:程序博客网 时间:2024/06/05 19:25
如何用在浏览器端预览本地文件
今天的主题是使用浏览器预览本地文件。
由于浏览器安全策略的限制,javascript程序不能自由地访问本地资源,这是对用户信息安全来说,是一项不得不遵守的准则。假如网络上的javascript程序可以自如地访问用户主机的本地资源,那么浏览器用户将毫无安全可言。尽管有这个安全限制,但是在得到用户允许的情况下,浏览器还是可以访问本地资源的。
获得用户允许的方法就是通过标签来让用户手动选择文件,这一过程就是用户授予访问权限的过程。然后 使用获得File 对象通过URL.createObjectURL(file)转换为文件url, 就可以传递给类似y于img,video,audio等标签使用了。我将本地文件转换为url 的功能封装成了一个类。
function LocalFileUrl(){ var _this = this; this.input_id = 'input_getLocalFile'+ Math.round(Math.random() * 1000); $("body").append("<input type='file' style='display:none' id='"+this.input_id+"' multiple>"); this.urls=[]; var _this = this; $("#" + this.input_id).change(function(e){ console.log("change"); //如果_this.urls 不为空,则释放url if(_this.urls){ _this.urls.forEach(function(url,index,array){ URL.revokeObjectURL(url.url);//一经释放,马上将无法使用这个url的资源 }); _this.urls = []; } $(this.files).each(function(index,file){ var url = URL.createObjectURL(file); _this.urls.push({url:url,file:file}); }); typeof _this.getted == 'function' && _this.getted(_this.urls); })}/*参数说明:getted:function(urls){}urls是一个url对象数组。[url]url = {url:url, //选取的文件urlfile:file //选取的文件对象引用}*/LocalFileUrl.prototype.getUrl = function(getted){ this.getted = getted; $("#"+ this.input_id).click();}
使用方法:
var localFileUrl = new LocalFileUrl();//实例化对象//触发读取,弹出文件选择框,并且监听文件选择事件。 localFileUrl.getUrl(function(urls){ urls.forEach(function(item,index,array){ $("body").append("<div>"+index+"----"+item.url+"</div>") })})
使用jQuery 的promise对象改写
这种方式的好处是可以使用链式写法,并且可以绑定多个done事件处理函数,执行顺序按照绑定顺序。
function LocalFileUrl(){ this.dtd ={}; this.input_id = 'input_getLocalFile'+ Math.round(Math.random() * 1000); $("body").append("<input type='file' style='display:none' id='"+this.input_id+"' multiple>"); this.urls=[]; var _this = this; $("#" + this.input_id).change(function(e){ //如果_this.urls 不为空,则释放url if(_this.urls){ _this.urls.forEach(function(url,index,array){ URL.revokeObjectURL(url.url);//一经释放,马上将无法使用这个url的资源 }); _this.urls = []; } $(this.files).each(function(index,file){ var url = URL.createObjectURL(file); _this.urls.push({url:url,file:file}); }); //传入一个可选的参数数组 _this.dtd.resolveWith(window,new Array(_this.urls)); })}/*返回一个jquery 的promise 对象,可以绑定done()事件。done(urls)接收一个urls数组{ url:url, file:file// 选取的文件对象}*/LocalFileUrl.prototype.getUrl = function(){ this.dtd = $.Deferred(); $("#"+ this.input_id).click(); return this.dtd.promise();}
使用方式
var localFilrUrl = new LocalFileUrl();// 绑定done事件localFileUrl.getUrl().done(function(urls){ urls.forEach(function(item,index,array){ $("body").append("<div>"+index+"----"+item.url+"</div>") })}).done(function(){ console.log("完成");}).done(function(){ alert("已经读取了本地文件路径");})
兼容性
URL.createObjectURL(File/Blob)是一个实验性的功能。IE10及以上版本兼容。与之对应的是URL.revokeObjectURL(url),它用来告诉浏览器已经不需要这个url的引用了,可以释放掉了。一经调用,这个url立即失效。
阅读全文
0 0
- js读取本地文件
- js读取本地文件
- js读取本地文件(IE)
- JS 读取本地文件
- js读取本地文件
- js读取本地文件
- JS学习--用JS读取本地文件
- JS读取本地文件及目录的方法
- js依次读取本地文件并显示内容
- JS读取本地文件及目录的方法
- JS读取本地文件及目录的方法
- JS读取本地文件及目录的方法
- JS读取本地文件及目录的方法
- JS读取本地文件及目录的方法
- js 读取本地文件显示在 img中
- JS创建、写入、读取本地文件(txt)
- 读取本地文件
- 读取本地文件
- 如果有人问你爬虫抓取技术的门道,请叫他来看这篇文章
- 多线程socket通信,信号量控制发送多线程发送顺序,测试通信延时
- Ciclop开源3D扫描仪软件---Horus源码分析之src\horus\engine\calibration\autocheck.py
- R语言
- 单体模式
- js读取本地文件
- 国际范儿的教育云
- 信号与系统常用matlab函数
- ExtJs checkbox绑定click事件实现全选
- videoView和mediaController视频播放
- HDU-1255 覆盖的面积 (线段树 求矩形覆盖面积)
- cs231n笔记(5)--传统神经网络,激活函数
- 网络Cisco实验-同一交换机不同VLAN通信
- Til the Cows Come Home POJ