<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script>
// 图片地址 转为 blob 、base64格式
//imgurl 图片url
function getBase64ByURL (imgUrl) {
return new Promise(resolve => {
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) {
var blob = this.response;
let oFileReader = new FileReader();
oFileReader.onloadend = function (e) {
resolve({blob,base64:e.target.result})
};
oFileReader.readAsDataURL(blob);
}
};
xhr.send();
});
};
// 调用
getBase64ByURL('https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fdingyue.ws.126.net%2F2020%2F0216%2Fb3dd1807j00q5sq0u0043c000xc00i3m.jpg&refer=http%3A%2F%2Fdingyue.ws.126.net&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1648796493&t=c4ff3ffc4e775be8a039a0d9aa8c043a').then(res=>{
let {blob,base64} = res
console.log(blob,base64,"blob,base64")
});
</script>
</head>
<body>
</body>
</html>
JS图片地址 转为 blob 、base64格式
最新推荐文章于 2024-06-21 15:25:50 发布