HTML添加上传图片并进行预览

使用说明:新建文件,直接复制粘贴,保存文件为html 格式,在浏览器运行即可;

第一种:

1<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 2<html> 3 <head> 4 <title> New Document </title> 5 </head> 6 7 <script src="http://i.gtimg.cn/qzone/biz/gdt/lib/jquery/jquery-2.1.4.js?max_age=31536000"></script> 8 9 <script> 10 11 $(function(){ 12 $('#upLoad').on('change',function(){ 13 var filePath = $(this).val(), //获取到input的value,里面是文件的路径 14 fileFormat = filePath.substring(filePath.lastIndexOf(".")).toLowerCase(), 15 imgBase64 = '', //存储图片的imgBase64 16 fileObj = document.getElementById('upLoad').files[0]; //上传文件的对象,要这样写才行,用jquery写法获取不到对象 17 18 // 检查是否是图片 19 if( !fileFormat.match(/.png|.jpg|.jpeg/) ) { 20 alert('上传错误,文件格式必须为:png/jpg/jpeg'); 21 return; 22 } 23 24 // 调用函数,对图片进行压缩 25 compress(fileObj,function(imgBase64){ 26 imgBase64 = imgBase64; //存储转换的base64编码 27 $('#viewImg').attr('src',imgBase64); //显示预览图片 28 }); 29 }); 30 31 // 不对图片进行压缩,直接转成base64 32 function directTurnIntoBase64(fileObj,callback){ 33 var r = new FileReader(); 34 // 转成base64 35 r.onload = function(){ 36 //变成字符串 37 imgBase64 = r.result; 38 console.log(imgBase64); 39 callback(imgBase64); 40 } 41 r.readAsDataURL(fileObj); //转成Base64格式 42 } 43 44 // 对图片进行压缩 45 function compress(fileObj, callback) { 46 if ( typeof (FileReader) === 'undefined') { 47 console.log("当前浏览器内核不支持base64图标压缩"); 48 //调用上传方式不压缩 49 directTurnIntoBase64(fileObj,callback); 50 } else { 51 try { 52 var reader = new FileReader(); 53 reader.onload = function (e) { 54 var image = $('<img/>'); 55 image.load(function(){ 56 square = 700, //定义画布的大小,也就是图片压缩之后的像素 57 canvas = document.createElement('canvas'), 58 context = canvas.getContext('2d'), 59 imageWidth = 0, //压缩图片的大小 60 imageHeight = 0, 61 offsetX = 0, 62 offsetY = 0, 63 data = ''; 64 65 canvas.width = square; 66 canvas.height = square; 67 context.clearRect(0, 0, square, square); 68 69 if (this.width > this.height) { 70 imageWidth = Math.round(square * this.width / this.height); 71 imageHeight = square; 72 offsetX = - Math.round((imageWidth - square) / 2); 73 } else { 74 imageHeight = Math.round(square * this.height / this.width); 75 imageWidth = square; 76 offsetY = - Math.round((imageHeight - square) / 2); 77 } 78 context.drawImage(this, offsetX, offsetY, imageWidth, imageHeight); 79 var data = canvas.toDataURL('image/jpeg'); 80 //压缩完成执行回调 81 callback(data); 82 }); 83 image.attr('src', e.target.result); 84 }; 85 reader.readAsDataURL(fileObj); 86 }catch(e){ 87 console.log("压缩失败!"); 88 //调用直接上传方式 不压缩 89 directTurnIntoBase64(fileObj,callback); 90 } 91 } 92 } 93}); 94 95 </script> 96 97 <body> 98 <input type="file" id="upLoad" name="image" > 99 <!-- 显示上传之后的图片 --> 100 <div id='previewImg'> 101 <img src="" id='viewImg'/> 102 </div> 103</body> 104</html>

 第二种:

1<!doctype html> 2<html lang="en"> 3<head> 4 <meta charset="UTF-8"> 5 <title>Document</title> 6</head> 7<body> 8<input type="file"><br> 9<img src="" height="200" alt="Image preview area..." title="preview-img"> 10<script> 11 var fileInput = document.querySelector('input[type=file]'), 12 previewImg = document.querySelector('img'); 13 fileInput.addEventListener('change', function () { 14 var file = this.files[0]; 15 var reader = new FileReader(); 16 // 监听reader对象的的onload事件,当图片加载完成时,把base64编码賦值给预览图片 17 reader.addEventListener("load", function () { 18 previewImg.src = reader.result; 19 }, false); 20 // 调用reader.readAsDataURL()方法,把图片转成base64 21 reader.readAsDataURL(file); 22 }, false); 23</script> 24</body> 25</html>
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

AndroidStudio封装SDK的那些事

<divclass"markdown\_views"<!flowchart箭头图标勿删<svgxmlns"http://www.w3.org/2000/svg"style"display:none;"<pathstrokelinecap"round"d"M5,00,2.55,5z"id"raphael

HTML添加上传图片并进行预览 - HelloWorld