当前位置:网站首页>将input type='file' 类型的图片文件转成base64
将input type='file' 类型的图片文件转成base64
2022-07-28 16:27:00 【梁毅】
带有图片的form表单上传数据是很麻烦的,因为图片通常都是和文字分开上传,这是很麻烦的,所有吧图片转成base64就可以和当成文字上传了。话不多少,看代码:
首先定义一个类型为file的input标签还要定义一个onchange事件,并传入一个event参数。
<div> <input type="file" id="imgTest" type="file" οnchange="imgChange(event)" accept=".gif,.jpg,.jpeg,.png"> </div> <img src="" id="showImage" alt="">
在这个onchange事件方法中会接收一个event参数,我们可以在这个参数中拿到这个input的标签对象还可以拿到这个文件。
function imgChange(e) { console.info(e.target.files[0]);//图片文件 var dom =$("input[id^='imgTest']")[0]; console.info(dom.value);//这个是文件的路径 C:\fakepath\icon (5).png console.log(e.target.value);//这个也是文件的路径和上面的dom.value是一样的 var reader = new FileReader(); reader.onload = (function (file) { return function (e) { console.info(this.result); //这个就是base64的数据了 var sss=$("#showImage"); $("#showImage")[0].src=this.result; }; })(e.target.files[0]); reader.readAsDataURL(e.target.files[0]);
拓展 : 把页面中的图片变成base64的数据
<!--这里先搞上一张图片--> <img id="img" src="/img/my.jpg" alt="" style="width: 150px;height: 150px"> <input type="button" οnclick="imgChaneBase64()" value="img标签绘制到canvas" /><br /> <!--引入html2canvas--> <script type="text/javascript" src="/lib/html2canvas/html2canvas.js"></script> <script type="text/javascript"> function imgChaneBase64() { //将要变成base64的图片的html标签当参数传进来,这里如果将document传进来,就会将整个页面变成图片,在then中会返回一个canvas html2canvas(document.getElementById("img")).then(function(canvas) { console.info(canvas); var imgData = canvas.toDataURL("image/png"); //这里会将canvas转化为base64的数据 document.body.appendChild(canvas); //页面鼠标右击这个标签时会提示保存图片,保存之后会以png格式保存 }); } </script>这里要注意的是这种方法其实利用的是截屏的原理,将html内容变成base64的,所有其实不是真的将这个图片变成base64。
边栏推荐
- Verilog 每日一题 (VL24 多bit MUX同步器 跨时域输出)
- Some attention code explanations
- 堡垒机的作用
- 深度分享阿里(蚂蚁金服)技术面试流程,附前期准备,学习方向
- Iris framework practice of goweb development: project summary and review
- C语言实现扫雷小游戏
- [CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring
- Goweb开发之Beego框架实战:第三节 程序执行流程分析
- Verilog 每日一题 (VL27 可置位计数器)
- Selection of resistance in high speed circuit
猜你喜欢

Verilog daily question (vl28 plus and minus counter)
![[CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring](/img/6a/bbc1ab0cfae9139308da4ded1376a8.png)
[CDH] configure CDH components through clouderamanager and collect JMX information with Prometheus monitoring

Zero foundation uses unity3d to develop AR applications and download 3D models remotely

wpf命令按钮透明样式

Verilog 每日一题 (VL24 多bit MUX同步器 跨时域输出)

Use Alibaba cloud's free SSL certificate

Verilog daily question (vl24 multi bit MUX synchronizer cross time domain output)

Verilog 每日一题(VL26 简易秒表)

The practice of beego framework developed by goweb: Section 4 database configuration and connection

Verilog daily question (vl4 shift operation and multiplication)
随机推荐
Verilog 每日一题(VL4 移位运算与乘法)
What does the service grid that has been popular for two years bring to microservices? (Reprinted)
Goweb开发之Beego框架实战:第五节 项目搭建及注册用户
【presto 】presto 新版本升级详情
循环中使用switch在执行条件后使用break和continue的区别
MySQL的触发器
Editor MAVON editor for offline use
The actual combat of the beego framework of goweb development: Section III program execution process analysis
【atlas】atlas 编译报错整理(全)
Master JVM interview topics and answers offer get soft (with learning roadmap)
线性代数及矩阵论(十)
Verilog daily question (vl27 settable counter)
Asynchronous circuit design -- principle and example of synchronous pulser
Linear algebra and matrix theory (IX)
[kibana] problem sorting kibana 7.x no indices match pattern "APM-*“
Firewalld防护墙
Goweb开发之Beego框架实战:第一节 Beego框架介绍
带参数的微信小程序二维码生成
QR code generation of wechat applet with parameters
Microservice Architecture - service registry and service gateway (6.8) (Reprint)