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上

 1 <div><input id="upload" type="file"></div>
 2 <div><img id="pic" src=""></div>
 3 <script>
 4     var reader = new FileReader();
 5     reader.onload = function(e){
 6         document.getElementById(‘pic‘).setAttribute(‘src‘, e.target.result);
 7     };
 8     function readURL(input) {
 9         if (input.files && input.files[0]) {
10             reader.readAsDataURL(input.files[0]);
11         }
12     };
13     document.getElementById(‘upload‘).onchange = function(){
14         readURL(this);
15     };
16 </script>
时间: 2024-10-10 07:50:36

HTML5图片上传预览的相关文章

JS实现图片上传预览效果:方法一

<script type="text/javascript"> //处理file input加载的图片文件 $(document).ready(function(e) { //判断浏览器是否有FileReader接口 if(typeof FileReader =='undefined') { /*$("#images_show").css({'background':'none'}).html('亲,您的浏览器还不支持HTML5的FileReader接口

来之不易的js图片上传预览代码

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getS

JavaScript 图片上传预览效果

图片上传预览是一种在图片上传之前对图片进行本地预览的技术.使用户选择图片后能立即查看图片,而不需上传服务器,提高用户体验.但随着浏览器安全性的提高,要实现图片上传预览也越来越困难.不过群众的智慧是无限的,网上已经有很多变通或先进的方法来实现.例如ie7/ie8的滤镜预览法,firefox 3的getAsDataURL方法.但在opera.safari和chrome还是没有办法实现本地预览,只能通过后台来支持预览.在研究了各种预览方法后,作为总结,写了这个程序,跟大家一起分享.上次写的简便无刷新文

js:s上次预览,上传图片预览,图片上传预览

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

html,图片上传预览,input file获取文件等相关操作

input file常用方法: var obj=document.getElementById("upimage"); var file=obj.files[0];//获取文件数据 var path=obj.value;//获取文件当前路径 var size=obj.files[0].size;//获取文件大小 var prefix=path.substring( path.lastIndexOf('\\')+1 );//获取文件名的前缀名(文件格式) var suffix=path.

js前端实现多图图片上传预览

<html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>测试页面</title> <script type="text/javascript"> //下面用于

Javascript之图片上传预览

使用Javascript之图片上传预览,我们无需上传到服务器中,兼容所有浏览器. 关键方法是使用微软库filter:progid:DXImageTransform.Microsoft.AlphaImageLoader来生成本地图片预览图的. <!DOCTYPE html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" /> <

多图片上传预览功能

//下面用于多图片上传预览功能 function setImagePreviews(avalue) { var docObj = document.getElementById("doc"); var dd = document.getElementById("dd"); dd.innerHTML = ""; var fileList = docObj.files; for (var i = 0; i < fileList.length;

移动端图片上传预览

前天要做wap版的图片上传预览,找了好半天才找到比较适合的插件,我在该插件的基础上修改了一些东西,比如:上传后的图片删除后不能再添加.不能限制上传图片的数量. input虽然有multiple(多选),但是android目前是不支持的. 该插件控制不了不能上传同一张图片,暂时没有思路解决这个问题(:′д`)ゞ 1 <!DOCTYPE html> 2 <html lang="zh-cn"> 3 4 <head> 5 <meta charset=&