当前位置:网站首页>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 .
边栏推荐
- AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
- 课设之百万数据文档存取
- 请问,redis没有消费消息,都在redis里堆着是怎么回事?用的是cerely 。
- wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
- libSGM的horizontal_path_aggregation程序解读
- PAG体验:十分钟完成AE动效部署上线各平台!
- Démontage de la fonction du système multi - Merchant Mall 01 - architecture du produit
- IP address home location query
- IP address home location query full version
- Navigation - are you sure you want to take a look at such an easy-to-use navigation framework?
猜你喜欢
Simple use of websocket
【愚公系列】2022年7月 Go教学课程 005-变量
今日睡眠质量记录78分
潘多拉 IOT 开发板学习(HAL 库)—— 实验12 RTC实时时钟实验(学习笔记)
Parsing of XML files
[Reading stereo matching papers] [III] ints
JS get the current time, month, day, year, and the uniapp location applet opens the map to select the location
SAKT方法部分介绍
Vscode configuration uses pylint syntax checker
js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
随机推荐
ndk初学习(一)
gvim【三】【_vimrc配置】
Ascend 910实现Tensorflow1.15实现LeNet网络的minist手写数字识别
【愚公系列】2022年7月 Go教学课程 005-变量
Mrs offline data analysis: process OBS data through Flink job
数据湖(九):Iceberg特点详述和数据类型
UML state diagram
wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
PLC:自动纠正数据集噪声,来洗洗数据集吧 | ICLR 2021 Spotlight
一款你不容错过的Laravel后台管理扩展包 —— Voyager
Oracle non automatic submission solution
Equipment failure prediction machine failure early warning mechanical equipment vibration monitoring machine failure early warning CNC vibration wireless monitoring equipment abnormal early warning
小程序目录结构
[network security] SQL injection syntax summary
Leetcode——剑指 Offer 05. 替换空格
一个程序员的水平能差到什么程度?尼玛,都是人才呀...
一文读懂数仓中的pg_stat
设备故障预测机床故障提前预警机械设备振动监测机床故障预警CNC震动无线监控设备异常提前预警
Excuse me, does PTS have a good plan for database pressure measurement?
The longest ascending subsequence model acwing 1014 Mountaineering