当前位置:网站首页>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便于后端处理 运行速度上也有优势
边栏推荐
- 互联网行业最佳产品开发流程 推荐!
- 283.移动零
- [ta - Frost Wolf May - 100 people plan] 2.3 Introduction aux fonctions communes
- Edge浏览器的小技巧:Enter+Ctrl可以自动将地址栏转换为网址
- MySQL function variable stored procedure
- [TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation
- 72. edit distance
- LeetCode 1828. Count the number of points in a circle
- 674. longest continuous increasing sequence force buckle JS
- 【TA-霜狼_may-《百人计划》】1.2.1 向量基础
猜你喜欢

Obtain detailed ideas for ABCDEF questions of 2022 American Games

类和对象收尾

Why can't you find the corresponding function by clicking go to definiton (super easy has a diagram)

MallBook:后疫情时代下,酒店企业如何破局?

Visit the image URL stored by Alibaba cloud to preview the thumbnail directly on the web page instead of downloading it directly

25.k sets of flipped linked lists

It's settled! 2022 JD cloud summit of JD global technology Explorer conference see you in Beijing on July 13

【人话版】WEB3黑暗森林中的隐私博弈

创新界,聚势行 | 2022人大金仓“百城巡展”火热开启

嵌入式系统开发笔记79:为什么要获取本机网卡IP地址
随机推荐
嵌入式系统开发笔记81:使用Dialog组件设计提示对话框
Use selenium automated test tool to climb the enrollment score line and ranking of colleges and universities related to the college entrance examination
[TA frost wolf \u may- hundred talents plan] 1.2.3 MVP matrix operation
【TA-霜狼_may-《百人計劃》】1.2.1 向量基礎
TASK04|数理统计
这可能是你进腾讯最后的机会了..
214. minimum palindrome string
盘点华为云GaussDB(for Redis)六大秒级能力
171. excel table column No
Concurrent mode of different performance testing tools
409. longest palindrome
嵌入式系统开发笔记79:为什么要获取本机网卡IP地址
【人话版】WEB3黑暗森林中的隐私博弈
创新界,聚势行 | 2022人大金仓“百城巡展”火热开启
Spock单元测试框架介绍及在美团优选的实践___第一章
嵌入式系統開發筆記80:應用Qt Designer進行主界面設計
Usage of AfxMessageBox and MessageBox
[ta- frost wolf \u may- hundred people plan] 2.2 model and material space
[recommended algorithm] C interview question of a small factory
程序员女友给我做了一个疲劳驾驶检测