当前位置:网站首页>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便于后端处理 运行速度上也有优势
边栏推荐
- 高并发下接口幂等性如何保证?
- [Master / slave] router election in DD message
- Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
- 多次跳槽后,月薪等于老同事的年薪
- Hololens2 development environment building and deploying apps
- 【历史上的今天】6 月 30 日:冯·诺依曼发表第一份草案;九十年代末的半导体大战;CBS 收购 CNET
- 389. find a difference
- Tip of edge browser: enter+ctrl can automatically convert the address bar into a web address
- 【TA-霜狼_may-《百人计划》】1.1 渲染流水线
- 使用WinMTR软件简单分析跟踪检测网络路由情况
猜你喜欢
![[send email with error] 535 error:authentication failed](/img/58/8cd22fed1557077994cd78fd29f596.png)
[send email with error] 535 error:authentication failed

盘点华为云GaussDB(for Redis)六大秒级能力

多次跳槽后,月薪等于老同事的年薪

Procurement intelligence is about to break out, and Alipay'3+2'system helps enterprises build core competitive advantages

Programs and processes, process management, foreground and background processes

使用WinMTR软件简单分析跟踪检测网络路由情况

Recommend the best product development process in the Internet industry!

Concurrent mode of different performance testing tools

[TA frost wolf \u may - "hundred people plan"] 2.1 color space

Do280 management application deployment --rc
随机推荐
Why can't you find the corresponding function by clicking go to definiton (super easy has a diagram)
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
一些小知识点
NFT: utilisez EIP - 2981 pour commencer un voyage de redevances NFT
72. edit distance
[human version] Web3 privacy game in the dark forest
Network metering - application layer
Edge浏览器的小技巧:Enter+Ctrl可以自动将地址栏转换为网址
Leetcode learning - day 36
It's settled! 2022 JD cloud summit of JD global technology Explorer conference see you in Beijing on July 13
创新界,聚势行 | 2022人大金仓“百城巡展”火热开启
Recommend the best product development process in the Internet industry!
165. compare version numbers
Quickly filter data such as clock in time and date: Excel filter to find whether a certain time point is within a certain time period
Unity's 3D multi-point arrow navigation
168. excel table column name
熊市下的Coinbase:亏损、裁员、股价暴跌
[TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation
多次跳槽后,月薪等于老同事的年薪
6. zigzag transformation