当前位置:网站首页>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 .
边栏推荐
- C # switch pages through frame and page
- bashrc与profile
- 小米的芯片自研之路
- Assign a dynamic value to the background color of DataGrid through ivalueconverter
- Nllb-200: meta open source new model, which can translate 200 languages
- oracle 非自动提交解决
- Search engine interface
- 一文读懂数仓中的pg_stat
- 解析PHP跳出循环的方法以及continue、break、exit的区别介绍
- ES日志报错赏析-maximum shards open
猜你喜欢
一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
数据流图,数据字典
GAN发明者Ian Goodfellow正式加入DeepMind,任Research Scientist
VSCode 配置使用 PyLint 语法检查器
Docker deploy Oracle
OAuth 2.0 + JWT protect API security
潘多拉 IOT 开发板学习(HAL 库)—— 实验12 RTC实时时钟实验(学习笔记)
Notes de l'imprimante substance: paramètres pour les affichages Multi - écrans et multi - Résolutions
STM32CubeMX,68套组件,遵循10条开源协议
libSGM的horizontal_path_aggregation程序解读
随机推荐
Million data document access of course design
请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
Regular expression integer positive integer some basic expressions
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
Excuse me, as shown in the figure, the python cloud function prompt uses the pymysql module. What's the matter?
[Reading stereo matching papers] [III] ints
Ian Goodfellow, the inventor of Gan, officially joined deepmind as research scientist
【愚公系列】2022年7月 Go教学课程 005-变量
Beginner JSP
多商户商城系统功能拆解01讲-产品架构
JS get the current time, month, day, year, and the uniapp location applet opens the map to select the location
Horizontal of libsgm_ path_ Interpretation of aggregation program
NDK beginner's study (1)
小米的芯片自研之路
Leetcode - Sword finger offer 05 Replace spaces
ES日志报错赏析-maximum shards open
属性关键字ServerOnly,SqlColumnNumber,SqlComputeCode,SqlComputed
Search engine interface
【服务器数据恢复】某品牌StorageWorks服务器raid数据恢复案例
Navigation — 这么好用的导航框架你确定不来看看?