html5读取本地文件示例代码
发布时间:2014-04-22 15:40:51 作者:佚名
我要评论
这篇文章主要介绍了html5读取本地文件的具体实现,html结构样式、Css样式及js代码如下,需要的朋友可以看看哦
html结构样式如下:
<div class="addpic">
<button>添加图片</button>
<form>
<input id="logoimg" class="addlogo" type="file" multiple accept="image/*" name="logo">
</form>
</div>
<img id="showlogo" src="" alt="">
从样式上说应不显示input元素的输入框,这时需将input设置为透明样式,然后将其覆盖到button元素上方,这时方可实现点击button上传图片。将accepted设置为“image/*”,则只允许图片类文件上传。
Css样式如下
.addpic{
position:relative;
margin-left:100px;
width:95px;
height:30px;
}
.addlogo {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
cursor: pointer;
font-size: 30px;
opacity: 0;
position: absolute;
right: 0;
top: 0;
z-index: 10;
}
js代码
function readFiles(evt){
var files=evt.target.files;
if(!files){
console.log("the file is invaild");
return;
}
for(var i=0, file; file=files[i]; i++){
var imgele=new Image();
var thesrc=window.URL.createObjectURL(file);
imgele.src=thesrc;
imgele.onload=function(){
$("#showlogo").attr("src",this.src);
}
}
}
$(document).ready(function(){
$("#logoimg").change(function(e){
readFiles(e)
});
});
复制代码
代码如下:<div class="addpic">
<button>添加图片</button>
<form>
<input id="logoimg" class="addlogo" type="file" multiple accept="image/*" name="logo">
</form>
</div>
<img id="showlogo" src="" alt="">
从样式上说应不显示input元素的输入框,这时需将input设置为透明样式,然后将其覆盖到button元素上方,这时方可实现点击button上传图片。将accepted设置为“image/*”,则只允许图片类文件上传。
Css样式如下
复制代码
代码如下:.addpic{
position:relative;
margin-left:100px;
width:95px;
height:30px;
}
.addlogo {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
cursor: pointer;
font-size: 30px;
opacity: 0;
position: absolute;
right: 0;
top: 0;
z-index: 10;
}
js代码
复制代码
代码如下:function readFiles(evt){
var files=evt.target.files;
if(!files){
console.log("the file is invaild");
return;
}
for(var i=0, file; file=files[i]; i++){
var imgele=new Image();
var thesrc=window.URL.createObjectURL(file);
imgele.src=thesrc;
imgele.onload=function(){
$("#showlogo").attr("src",this.src);
}
}
}
复制代码
代码如下:$(document).ready(function(){
$("#logoimg").change(function(e){
readFiles(e)
});
});
相关文章
- IndexedDB 是一种低级API,用于客户端存储大量结构化数据(包括, 文件/ blobs)。下面通过本文重点给大家介绍HTML5本地存储之IndexedDB的相关知识,感兴趣的朋友一起看看吧2017-06-16
- 我们在做页面时会用到本地存储的时候,今天说说localStorage本地存储。感兴趣的朋友一起学习吧2017-06-16
- html5的两种存储技术的最大区别就是生命周期,接下来通过本文给大家分享HTML5 LocalStorage 本地存储刷新值还在问题以及使用方法小结,需要的的朋友参考下本文吧2017-03-10
- 本篇文章主要介绍了HTML5 LocalStorage 本地存储 ,HTML5 storage提供了一种方式让网站能够把信息存储到你本地的计算机上,并再以后需要的时候进行获取。有兴趣的可以了解2016-12-23
- 这篇文章主要介绍了html5本地存储 localStorage操作使用详解的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下2016-09-20
- 这篇文章主要介绍了Html5 web本地存储实例详解的相关资料,需要的朋友可以参考下2016-07-28
- 下面小编就为大家带来一篇深入剖析webstorage[html5的本地数据处理]。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,祝大家游戏愉快哦2016-07-11
- 这篇文章主要为大家详细介绍了HTML5本地存储之Web Storage的相关资料,Web Storage如何用JSON存储多个数据,感兴趣的小伙伴们可以参考一下2016-07-04
- 下面小编就为大家带来一篇HTML5 本地存储 LocalStorage详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2016-06-24
- 在本地数据库中我们可以直接利用JavaScript创建数据库,并利用SQL语句执行相关的数据库操作。对于复杂的数据库,HTML5使用本地数据库进行操作,需要的朋友可以参考下2016-04-26


最新评论