当前位置:网站首页>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 .
边栏推荐
猜你喜欢
2022pagc Golden Sail award | rongyun won the "outstanding product technology service provider of the year"
Navigation — 这么好用的导航框架你确定不来看看?
Pert diagram (engineering network diagram)
Vscode configuration uses pylint syntax checker
PyTorch模型训练实战技巧,突破速度瓶颈
华为云数据库DDS产品深度赋能
数据湖(九):Iceberg特点详述和数据类型
内部排序——插入排序
Hands on Teaching: XML modeling
Démontage de la fonction du système multi - Merchant Mall 01 - architecture du produit
随机推荐
Oracle Linux 9.0 正式发布
IP address home location query full version
一文读懂数仓中的pg_stat
Parsing of XML files
Applet directory structure
GAN发明者Ian Goodfellow正式加入DeepMind,任Research Scientist
Differences between cookies and sessions
Ascend 910实现Tensorflow1.15实现LeNet网络的minist手写数字识别
CVPR2022 | 医学图像分析中基于频率注入的后门攻击
When FC connects to the database, do you have to use a custom domain name to access it outside?
c#通过frame 和 page 切换页面
PERT图(工程网络图)
GVIM [III] [u vimrc configuration]
Use case diagram
ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
Substance painter notes: settings for multi display and multi-resolution displays
Similarities and differences between switches and routers
oracle 触发器实现级联更新
SAKT方法部分介绍
Seven propagation behaviors of transactions