当前位置:网站首页>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便于后端处理 运行速度上也有优势
边栏推荐
- [TA frost wolf \u may- hundred people plan] 2.4 traditional empirical lighting model
- Possible problems and solutions of using scroll view to implement slider view
- Jenkins自动清理构建历史
- 浏览器顶部loading(来自知乎)
- 165. compare version numbers
- 【TA-霜狼_may-《百人计划》】2.2 模型与材质空间
- Recommend the best product development process in the Internet industry!
- 8. string conversion integer (ATOI)
- 互联网行业最佳产品开发流程 推荐!
- Go learning --- unit test subtest
猜你喜欢

Millet College wechat scanning code login process record and bug resolution

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

[TA frost wolf \u may - "hundred people plan"] 2.1 color space
![[ta- frost wolf \u may- hundred people plan] 1.1 rendering pipeline](/img/af/4498382bc47d8c9ae41c407b9d1265.png)
[ta- frost wolf \u may- hundred people plan] 1.1 rendering pipeline

In the innovation community, the "100 cities Tour" of the gold warehouse of the National People's Congress of 2022 was launched

LeetCode 1828. Count the number of points in a circle

The problem of integrating Alibaba cloud SMS: non static methods cannot be referenced from the static context

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

Use of JMeter counters

NFT:使用 EIP-2981 开启 NFT 版税之旅
随机推荐
Deep learning | rnn/lstm of naturallanguageprocessing
Analysis and case of pageobject mode
[shortcut key]
【TA-霜狼_may-《百人计划》】2.2 模型与材质空间
Qt开发经验小技巧226-230
【发送邮件报错】535 Error:authentication failed
陈宇(Aqua)-安全-&gt;云安全-&gt;多云安全
Introduction of Spock unit test framework and its practice in meituan optimization___ Chapter I
Unexpected token o in JSON at position 1, JSON parsing problem
431. 将 N 叉树编码为二叉树 DFS
不同性能测试工具的并发模式
嵌入式系統開發筆記80:應用Qt Designer進行主界面設計
10. regular expression matching
Redis (VII) optimization suggestions
互联网行业最佳产品开发流程 推荐!
Analyse et cas du modèle pageobject
Edge浏览器的小技巧:Enter+Ctrl可以自动将地址栏转换为网址
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
Chen Yu (Aqua) - Safety - & gt; Cloud Security - & gt; Multicloud security
Common thread methods and daemon threads