HTML5图片上传预览
HTML5实现图片的上传预览,需要使用FileReader对象。
FileReader: The FileReader object lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using File or Blob objects to specify the file or data to read.
也就是说,使用FileReader对象先读取用户需要上传的图片,这个时候,图片是保存在浏览器中的,然后通过设置img元素的src,来预览图片,方法很简单。
在使用FileReader时需要先弄明白其Event handlers和方法。
Event handlers
| Event handler | 描述 |
|---|---|
| FileReader.onabort | A handler for the abort event. This event is triggered each time the reading operation is aborted. |
| FileReader.onerror | A handler for the error event. This event is triggered each time the reading operation encounter an error. |
| FileReader.onload | A handler for the load event. This event is triggered each time the reading operation is successfully completed. |
| FileReader.onloadstart | A handler for the loadstart event. This event is triggered each time the reading is starting. |
| FileReader.onloadend | A handler for the loadend event. This event is triggered each time the reading operation is completed (either in success or failure). |
| FileReader.onprogress | A handler for the progress event. This event is triggered while reading a Blob content. |
Metchods
| Method | 描述 |
|---|---|
| FileReader.abort() | Aborts the read operation. Upon return, the readyState will be DONE. |
| FileReader.readAsArrayBuffer() | Starts reading the contents of the specified Blob, once finished, the result attribute contains an ArrayBuffer representing the file's data. |
| FileReader.readAsBinaryString() | Starts reading the contents of the specified Blob, once finished, the result attribute contains the raw binary data from the file as a string. |
| FileReader.readAsDataURL() | Starts reading the contents of the specified Blob, once finished, the result attribute contains a data: URL representing the file's data. |
| FileReader.readAsText() | Starts reading the contents of the specified Blob, once finished, the result attribute contains the contents of the file as a text string. |
所以只需要使用readAsDataURL()方法读取图片,绑定FileReader的onload事件,将读取的result中的url设置到img的src上
<div><input id="upload" type="file"></div>
<div><img id="pic" src=""></div>
<script>
var reader = new FileReader();
reader.onload = function(e){
document.getElementById('pic').setAttribute('src', e.target.result);
};
function readURL(input) {
if (input.files && input.files[0]) {
reader.readAsDataURL(input.files[0]);
}
};
document.getElementById('upload').onchange = function(){
readURL(this);
};
</script>
HTML5图片上传预览的更多相关文章
- HTML5 图片上传预览
<!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset="utf-8& ...
- [javascript]——移动端 HTML5 图片上传预览和压缩
在开发移动端web网页中,我们不可避免的会遇到文件上传的功能,但由于手机图片尺寸太大,上传时间过长导致用户体验太差,就需要在上传前对图片进行一定的压缩. 在代码之前,有必要先了解我们即将使用到的几个A ...
- html5 图片上传 预览
<html><body><fieldset> <legend>测试</legend> <div class="form-gr ...
- 用html5文件api实现移动端图片上传&预览效果
想要用h5在移动端实现图片上传&预览效果,首先要了解html5的文件api相关知识(所有api只列举本功能所需): 1.Blob对象 Blob表示原始二进制数据,Html5的file对象就继 ...
- 兼容好的JS图片上传预览代码
转 : http://www.codefans.net/articles/1395.shtml 兼容好的JS图片上传预览代码 (谷歌,IE11) <html xmlns="http:/ ...
- js实现图片上传预览及进度条
原文js实现图片上传预览及进度条 最近在做图片上传的时候,由于产品设计的比较fashion,上网找了比较久还没有现成的,因此自己做了一个,实现的功能如下: 1:去除浏览器<input type= ...
- html 图片上传预览
Html5 upload img 2012年12月27日 20:36 <!DOCTYPE HTML> <html> <head> <meta http-equ ...
- 模拟QQ心情图片上传预览
出于安全性能的考虑,目前js端不支持获取本地图片进行预览,正好在做一款类似于QQ心情的发布框,找了不少jquery插件,没几个能满足需求,因此自己使用SWFuplad来实现这个图片上传预览. 先粘上以 ...
- signup图片上传预览经常总结
html <html> <head> <meta charset="utf-8" /> <meta http-equiv="X- ...
随机推荐
- Log使用
学习参考:http://blog.csdn.net/hu_shengyang/article/details/6754031 log4j三种主要组件: logger记录对象 appender输出对象 ...
- Android应用之——微信微博第三方sdk登录分享使用过程中的一些常见问题
前言 近期在使用第三方登录和分享的过程中遇到了非常多问题,一方面能够归结为自己经验的不足,还有一方面事实上也说明了官方文档的含糊不清.这篇博文不会写关于怎样使用第三方登录分享,由于官方文档已经写明了步 ...
- 刚接触Joomla,写一下瞎折腾的初感受~
我这几天一直在苦苦寻找一款能够长期投靠的CMS产品,要求的是 1)必须支持命名空间 2)必须OOP + MVC分层 3)丰富分文档和使用群体,至少是出名的.免得哪一天他们解散了 4)-- 一開始我把目 ...
- Android图文混排-实现EditText图文混合插入上传
前段时间做了一个Android会议管理系统,项目需求涉及到EditText的图文混排,如图: 在上图的"会议详情"中.须要支持文本和图片的混合插入,下图演示输入的演示样例: 当会议 ...
- insmod hello.ko -1 Invalid module format最简单的解决的方法
在下也是从网上搜索到的这样的解决的方法. 遇到这样的情况后,通过dmesg看一下内核日志. 假设发现有例如以下日志.那就好办了. hello: version magic '2.6.33.3 ' sh ...
- 指针,c语言的灵魂
指针是一个值为内存地址的变量. 变量是一块内存空间,指针是变量,是用来存储内存地址的变量. #include <stdio.h> #include <stdlib.h> int ...
- hdoj--2098--分拆素数和(枚举)
分拆素数和 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Subm ...
- 剑指offer——02替换空格(Python3)
思路:Python列表中实现字符串的替换,涉及到频繁的插入操作,在数据结构中线性表分为顺序表和链表,顺序表的适合频繁的查询,链表适合频繁的插入和删除.综上所述,本题使用链表来实现. 我们从字符串的后面 ...
- Habernate配置一对一,一对多,多对多(二)
一.开篇 紧接着上篇的博客来写:http://www.cnblogs.com/WJ--NET/p/7845000.html(habernate环境的搭建) 二.配置一对一 2.1.新建客户类和公司类( ...
- 在64位linux上编译32位程序 for i386 intel
编辑中 # ld -V GNU ld version 2.15.92.0.2 20040927 Supported emulations: elf_x86_64 elf_i386 i386linux ...