当前位置:网站首页>Convert the image file of input type='file'to Base64
Convert the image file of input type='file'to Base64
2022-07-28 17:37:00 【yoki 】
With pictures form Uploading data from forms is troublesome , Because pictures are usually uploaded separately from text , It's a lot of trouble , Turn all pictures into base64 You can upload it as text . What words do not , Look at the code :
First define a type as file Of input The tag also defines a onchange event , And introduce a event Parameters .
<div> <input type="file" id="imgTest" type="file" οnchange="imgChange(event)" accept=".gif,.jpg,.jpeg,.png"> </div> <img src="" id="showImage" alt="">
In this onchange The event method will receive a event Parameters , We can get this in this parameter input The tag object of can also get this file .
function imgChange(e) { console.info(e.target.files[0]);// Picture file var dom =$("input[id^='imgTest']")[0]; console.info(dom.value);// The path to this file is C:\fakepath\icon (5).png console.log(e.target.value);// This is also the path of the file and the above dom.value It's the same var reader = new FileReader(); reader.onload = (function (file) { return function (e) { console.info(this.result); // This is base64 The data of the var sss=$("#showImage"); $("#showImage")[0].src=this.result; }; })(e.target.files[0]); reader.readAsDataURL(e.target.files[0]);
expand : Change the picture in the page into base64 The data of
<!-- Here's a picture first --> <img id="img" src="/img/my.jpg" alt="" style="width: 150px;height: 150px"> <input type="button" οnclick="imgChaneBase64()" value="img Draw labels to canvas" /><br /> <!-- introduce html2canvas--> <script type="text/javascript" src="/lib/html2canvas/html2canvas.js"></script> <script type="text/javascript"> function imgChaneBase64() { // It's going to be base64 The picture of html Tag when parameters are passed in , Here if you will document Come in , It will turn the whole page into a picture , stay then Will return a canvas html2canvas(document.getElementById("img")).then(function(canvas) { console.info(canvas); var imgData = canvas.toDataURL("image/png"); // Here will be canvas Turn into base64 The data of document.body.appendChild(canvas); // When you right-click this tab on the page, you will be prompted to save the picture , After saving, it will be saved with png Format preservation }); } </script>It should be noted here that this method actually uses the principle of screen capture , take html The content becomes base64 Of , All this is not really turning this picture into base64.
边栏推荐
- Vscode uses eslint prettier to format code automatically
- How important is baseline safety from non child websites
- Selection and application of inductors in high speed circuits
- Verilog 每日一题(VL4 移位运算与乘法)
- 渗透测试大杀器kali安装配置
- AMQP protocol details
- Master the key points of JVM interview
- 网上出现的js小数计算的除法优化
- Verilog daily question (VL2 asynchronous reset Series T trigger - Niuke network)
- Firewall protective wall
猜你喜欢

Jdwp unauthorized rapid utilization

【CDH】通过 ClouderaManager 配置CDH组件用 prometheus 监控采集JMX信息

Verilog daily question (vl29 single port RAM)

MySQL的触发器

R中因子(factor)

Selection and application of inductors in high speed circuits

clang format

With a total data volume of more than trillions of lines, Yuxi cigarette factory can easily deal with it by correctly selecting the timing database

Firewalld防护墙

一篇带你走近Kubernetes概貌与原理
随机推荐
LNMP source code compilation and installation
Verilog 每日一题(VL2 异步复位的串联T触发器--牛客网)
在PDF中插入文本水印
Verilog daily question (vl24 multi bit MUX synchronizer cross time domain output)
C#基础面试题(附答案)
JVM性能调优
掌握JVM面试专题和答案Offer拿到手软(附学习路线图)
关于标准IO缓冲区的问题
谈谈“发布后问题”的度量
C language to achieve minesweeping games
Linear algebra and matrix theory (7)
Verilog daily question (vl6 data series to parallel circuit)
High speed circuit design practice -- Overview
In some cases, error: (XX, XX) failed to resolve: XXXXXX.
Verilog 每日一题 (VL24 多bit MUX同步器 跨时域输出)
Vscode plug-in automatically adds comments
QR code generation of wechat applet with parameters
C # basic interview questions (with answers)
Zero foundation uses unity3d to develop AR applications and download 3D models remotely
The actual combat of the beego framework of goweb development: Section III program execution process analysis