当前位置:网站首页>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.
边栏推荐
- 谈谈你知道的发布上线(二)
- Opencv based real-time stitching of simple dual camera images
- MySQL detailed learning tutorial (recommended Collection)
- Azure Devops developed by visual studio 2015 team
- Embedded development learning path
- 关于标准IO缓冲区的问题
- Wechat applet cash red packet returns the error "the IP address is not the available IP address you set on the merchant platform". The ultimate solution
- SNAT、DNAT 防火墙规则的备份和还原
- 【kibana】问题整理 kibana 7.x No indices match pattern “apm-*“
- @RequestParam使用
猜你喜欢

Verilog daily question (vl27 settable counter)

LNMP源码编译安装

Encountered.Sqlite file processing during Android Development

简单易用的APP专项测试工具iTest4.7.0发布啦

AMQP协议详解

Azure Devops developed by visual studio 2015 team

Verilog daily question (vl5 signal generator)

掌握JVM面试专题和答案Offer拿到手软(附学习路线图)

从非儿童网站看基线安全到底有多重要

Jdwp unauthorized rapid utilization
随机推荐
[Presto] details of the new version upgrade of Presto
Verilog 每日一题 (VL30 RAM的简单实现)
Create a custom paging control
【atlas】atlas 编译报错整理(全)
Vscode uses eslint prettier to format code automatically
Visual studio 2012/2015 releases web applications together with.Cs source code
部署LAMP平台---Linux,Apache,MySQL,PHP的编译安装
Verilog 每日一题(VL8 使用generate…for语句简化代码)
clang format
简单易用的APP专项测试工具iTest4.7.0发布啦
@RequestParam使用
mysql实现按照自定义(指定顺序)排序
特殊质数js实现
高速电路设计实践——概述
AMQP协议详解
Deploy lamp platform -- compilation and installation of Linux, Apache, MySQL and PHP
Vscode plug-in automatically adds comments
Easy to use vscode plug-in memo
Convert multidimensional object array to one-dimensional array
100+医学影像数据集集锦