当前位置:网站首页>js 图片路径转换base64格式
js 图片路径转换base64格式
2022-07-01 04:13:00 【-耿瑞-】
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id = 'app'>
</div>
<script> let url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109242312005c1-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659174217&t=2fd6120db3cd74070561b7e1e112d0e7"; function getBase64(imgUrl) {
window.URL = window.URL || window.webkitURL; var xhr = new XMLHttpRequest(); xhr.open("get", imgUrl, true); // 至关重要 xhr.responseType = "blob"; xhr.onload = function () {
if (this.status == 200) {
//得到一个blob对象 var blob = this.response; // 至关重要 let oFileReader = new FileReader(); oFileReader.onloadend = function (e) {
let base64 = e.target.result; console.log(base64) }; oFileReader.readAsDataURL(blob); //====为了在页面显示图片,可以删除==== } } xhr.send(); } getBase64(url); </script>
</body>
</html>
运行结果如下
这样我们就成功转换路径为base64格式了 base64的路径直接放给图片路径就可以直接用了
颜色代码如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id = 'app'>
</div>
<script> let url = "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg.jj20.com%2Fup%2Fallimg%2F4k%2Fs%2F02%2F2109242312005c1-0-lp.jpg&refer=http%3A%2F%2Fimg.jj20.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1659174217&t=2fd6120db3cd74070561b7e1e112d0e7"; function getBase64(imgUrl) {
window.URL = window.URL || window.webkitURL; var xhr = new XMLHttpRequest(); xhr.open("get", imgUrl, true); // 至关重要 xhr.responseType = "blob"; xhr.onload = function () {
if (this.status == 200) {
//得到一个blob对象 var blob = this.response; // 至关重要 let oFileReader = new FileReader(); oFileReader.onloadend = function (e) {
let base64 = e.target.result; console.log(base64) document.getElementById('app').innerHTML = `<img src = ${
base64} />`; }; oFileReader.readAsDataURL(blob); //====为了在页面显示图片,可以删除==== } } xhr.send(); } getBase64(url); </script>
</body>
</html>

也是非常的简单 主要base64便于后端处理 运行速度上也有优势
边栏推荐
- 【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
- 674. longest continuous increasing sequence force buckle JS
- 242. valid Letter heteronyms
- Huawei simulator ENSP - hcip - Hybrid Experiment 2
- [untitled]
- Qt development experience tips 226-230
- LeetCode 1380. Lucky number in matrix
- [TA frost wolf \u may - "hundred people plan"] 2.1 color space
- Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
- [TA frost wolf _may - "hundred people plan"] 1.4 introduction to PC mobile phone graphics API
猜你喜欢
![[TA frost wolf \u may- hundred people plan] 1.3 secret of texture](/img/dc/ce2819258632cdcf2e130a4c05600e.png)
[TA frost wolf \u may- hundred people plan] 1.3 secret of texture

【TA-霜狼_may-《百人计划》】2.2 模型与材质空间

Embedded System Development Notes 79: why should I get the IP address of the local network card

283.移动零

【TA-霜狼_may-《百人计划》】1.2.1 向量基础
![[send email with error] 535 error:authentication failed](/img/58/8cd22fed1557077994cd78fd29f596.png)
[send email with error] 535 error:authentication failed
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET

206.反转链表

ThreeJS开篇

Usage of AfxMessageBox and MessageBox
随机推荐
Volley parsing data shows networking failure
陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
HoloLens2开发环境搭建及部署app
【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
Loop filtering based on Unet
Analyse et cas du modèle pageobject
After many job hopping, the monthly salary is equal to the annual salary of old colleagues
[shortcut key]
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
“目标检测“+“视觉理解“实现对输入图像的理解
Web components series (VIII) -- custom component style settings
Qt开发经验小技巧226-230
Edge浏览器的小技巧:Enter+Ctrl可以自动将地址栏转换为网址
[today in history] June 30: von Neumann published the first draft; The semiconductor war in the late 1990s; CBS acquires CNET
【发送邮件报错】535 Error:authentication failed
基于Unet的环路滤波
[ta - Frost Wolf May - 100 people plan] 1.2.1 base vectorielle
京东智能客服言犀意图体系搭建和意图识别技术介绍
242. valid Letter heteronyms
"Target detection" + "visual understanding" realizes the understanding of the input image