当前位置:网站首页>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.
边栏推荐
猜你喜欢

Esp-mqtt-at instruction connects Alibaba cloud Internet of things platform

Firewall protective wall

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

Verilog daily question (simple implementation of VL30 RAM)

深度分享阿里(蚂蚁金服)技术面试流程,附前期准备,学习方向

Random talk on test platform - platform construction ideas (Part 1)

蚂蚁金服移动测试工具solopi监控部分源码导读。。持续更新

Backup and restore of SNAT and DNAT firewall rules

Verilog daily question (vl26 simple stopwatch)

Can you read the story?
随机推荐
Talk about what you know about publishing online (I)
Verilog daily question (vl5 signal generator)
Gray code and binary conversion and typical examples (4bits gray code counter)
Easy to use vscode plug-in memo
一篇带你走近Kubernetes概貌与原理
Verilog 每日一题 (VL27 可置位计数器)
Encrypt the video and upload it to OSS to achieve high concurrent access
R中因子(factor)
Talk about the measurement of "post release problems"
Insert text watermark in PDF
【sqoop】sqoop1.4.7 安装集成CDH5.13
Shell脚本之免交互操作
在PDF中插入文本水印
数据库性能分析与优化(爱测未来团队内训材料)
strsplit()函数
区分ES6的export与Nodejs的module.exports的区别
Asynchronous circuit design -- principle and example of synchronous pulser
LNMP source code compilation and installation
Export word according to the template, generate compound format tables and variable column tables
Selection and application of inductors in high speed circuits