当前位置:网站首页>JS in the browser Base64, URL, blob mutual conversion
JS in the browser Base64, URL, blob mutual conversion
2022-07-07 14:28:00 【Snail games】
base64 The format is generally in data:image/jpeg;base64, A long string beginning with this similar form .
url Usually take the server address or path ( Form like http://xxx.jpg perhaps /static/xxx.jpg) Mainly , There will also be browser local addresses ( Form like blob:http:// such ).
blob It is generally an object type in the browser ,file Type is also one of them .
How to convert each other ?
base64 To blob:
such as png Format base64 Picture turned into blob type :
function pngBase64ToBlob(urlData) {
try {
var arr = urlData.split(',')
var mime = arr[0].match(/:(.*?);/)[1] || 'image/png';
// Get rid of url The head of the , And into byte
var bytes = window.atob(arr[1]);
// Handling exceptions , take ascii Code less than 0 The conversion of is greater than 0
var ab = new ArrayBuffer(bytes.length);
// Generate views ( Direct to memory ):8 Bit unsigned integer , length 1 Bytes
var ia = new Uint8Array(ab);
for (var i = 0; i < bytes.length; i++) {
ia[i] = bytes.charCodeAt(i);
}
return new Blob([ab], {
type: mime
});
}
catch (e) {
var ab = new ArrayBuffer(0);
return new Blob([ab], {
type: 'image/png',
});
}
}
Pictures in other formats are similar , So you can write a general picture interface :
function imageBase64ToBlob(urlData, type='image/jpeg') {
try {
var arr = urlData.split(',')
var mime = arr[0].match(/:(.*?);/)[1] || type;
// Get rid of url The head of the , And into byte
var bytes = window.atob(arr[1]);
// Handling exceptions , take ascii Code less than 0 The conversion of is greater than 0
var ab = new ArrayBuffer(bytes.length);
// Generate views ( Direct to memory ):8 Bit unsigned integer , length 1 Bytes
var ia = new Uint8Array(ab);
for (var i = 0; i < bytes.length; i++) {
ia[i] = bytes.charCodeAt(i);
}
return new Blob([ab], {
type: mime
});
}
catch (e) {
var ab = new ArrayBuffer(0);
return new Blob([ab], {
type: type,
});
}
}
blob Transfer to local url:
function blobToUrl(blob_data) {
return URL.createObjectURL(blob_data)
}
blob turn base64:
function blobToBase64(blob_data, callback) {
let reader = new FileReader()
reader.onload = (e) => {
if (callback) {
callback(e.target.result)
}
}
reader.readAsDataURL(blob_data)
}
url turn blob:
function urlToBlob(the_url, callback) {
let xhr = new XMLHttpRequest();
xhr.open("get", the_url, true);
xhr.responseType = "blob";
xhr.onload = function() {
if (this.status == 200) {
if (callback) {
callback(this.response);
}
}
};
xhr.send();
}
Encapsulating the above interfaces can realize the mutual conversion of the three formats .
边栏推荐
- UML state diagram
- Equipment failure prediction machine failure early warning mechanical equipment vibration monitoring machine failure early warning CNC vibration wireless monitoring equipment abnormal early warning
- Cargo placement problem
- 寺岗电子称修改IP简易步骤
- PLC:自动纠正数据集噪声,来洗洗数据集吧 | ICLR 2021 Spotlight
- KITTI数据集简介与使用
- Oracle Linux 9.0 officially released
- Data connection mode in low code platform (Part 2)
- c#通过frame 和 page 切换页面
- Démontage de la fonction du système multi - Merchant Mall 01 - architecture du produit
猜你喜欢

Selenium Library

Pert diagram (engineering network diagram)

Parsing of XML files

EfficientNet模型的完整细节

js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
![[Reading stereo matching papers] [III] ints](/img/d3/4238432492ac3dc4ec14a971b8848d.png)
[Reading stereo matching papers] [III] ints

数据流图,数据字典

LeetCode 648. Word replacement

2022pagc Golden Sail award | rongyun won the "outstanding product technology service provider of the year"

内部排序——插入排序
随机推荐
ES日志报错赏析-- allow delete
ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
The longest ascending subsequence model acwing 1014 Mountaineering
Transferring files between VMware and host
Is the spare money in your hand better to fry stocks or buy financial products?
全球首款 RISC-V 笔记本电脑开启预售,专为元宇宙而生!
小程序目录结构
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
bashrc与profile
gvim【三】【_vimrc配置】
PAG体验:十分钟完成AE动效部署上线各平台!
JS get the current time, month, day, year, and the uniapp location applet opens the map to select the location
Leetcode——236. The nearest common ancestor of binary tree
Substance painter notes: settings for multi display and multi-resolution displays
Substance Painter筆記:多顯示器且多分辨率顯示器時的設置
Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding
Cascading update with Oracle trigger
UML 顺序图(时序图)
Vscode configuration uses pylint syntax checker
找到自己的价值