当前位置:网站首页>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便于后端处理 运行速度上也有优势
边栏推荐
- 盘点华为云GaussDB(for Redis)六大秒级能力
- One job hopping up 8K, three times in five years
- 318. Maximum word length product
- Valid @suppresswarnings warning name
- 6. zigzag transformation
- [shortcut key]
- 京东智能客服言犀意图体系搭建和意图识别技术介绍
- Common thread methods and daemon threads
- 基于Unet的环路滤波
- [Master / slave] router election in DD message
猜你喜欢

NFT:使用 EIP-2981 开启 NFT 版税之旅

The programmer's girlfriend gave me a fatigue driving test

206.反转链表

Network metering - application layer
![[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

After many job hopping, the monthly salary is equal to the annual salary of old colleagues
![[leetcode skimming] February summary (updating)](/img/62/0d0d9f11434e49d33754a2e4f2ea65.jpg)
[leetcode skimming] February summary (updating)
![[recommended algorithm] C interview question of a small factory](/img/ae/9c83efe86c03763710ba5e4a2eea33.jpg)
[recommended algorithm] C interview question of a small factory

采购数智化爆发在即,支出宝'3+2'体系助力企业打造核心竞争优势

TASK04|数理统计
随机推荐
6. zigzag transformation
Millet College wechat scanning code login process record and bug resolution
【深度学习】(4) Transformer 中的 Decoder 机制,附Pytorch完整代码
【TA-霜狼_may-《百人计划》】1.2.1 向量基础
242. valid Letter heteronyms
“目标检测“+“视觉理解“实现对输入图像的理解
[TA frost wolf \u may- hundred talents plan] 1.2.2 matrix calculation
Redis (VII) optimization suggestions
盘点华为云GaussDB(for Redis)六大秒级能力
线程常用方法与守护线程
[leetcode skimming] February summary (updating)
ThreeJS开篇
206.反转链表
One job hopping up 8K, three times in five years
MallBook:后疫情时代下,酒店企业如何破局?
JD intelligent customer service Yanxi intention system construction and intention recognition technology introduction
熊市下的Coinbase:亏损、裁员、股价暴跌
Analysis and case of pageobject mode
互联网行业最佳产品开发流程 推荐!
Loop filtering based on Unet