当前位置:网站首页>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 .
边栏推荐
- Excusez - moi, l'exécution a été réussie lors de l'utilisation des données de puits SQL Flink à Kafka, mais il n'y a pas de nombre dans Kafka
- 低代码平台中的数据连接方式(下)
- 今日睡眠质量记录78分
- LeetCode 648. Word replacement
- IP address home location query full version
- js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
- PyTorch模型训练实战技巧,突破速度瓶颈
- ndk初学习(一)
- Leetcode——344. 反转字符串/541. 反转字符串 II/151. 颠倒字符串中的单词/剑指 Offer 58 - II. 左旋转字符串
- ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
猜你喜欢

Use case diagram

Mmkv use and principle

小程序目录结构

最长上升子序列模型 AcWing 482. 合唱队形

Hands on Teaching: XML modeling

Reverse non return to zero code, Manchester code and differential Manchester code of common digital signal coding

2022PAGC 金帆奖 | 融云荣膺「年度杰出产品技术服务商」

PyTorch模型训练实战技巧,突破速度瓶颈

The longest ascending subsequence model acwing 1014 Mountaineering

Data connection mode in low code platform (Part 2)
随机推荐
小程序目录结构
The longest ascending subsequence model acwing 1014 Mountaineering
Selenium库
Leetcode——236. 二叉树的最近公共祖先
LeetCode 648. 单词替换
多商户商城系统功能拆解01讲-产品架构
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
云上“视界” 创新无限 | 2022阿里云直播峰会正式上线
Internal sort - insert sort
When FC connects to the database, do you have to use a custom domain name to access it outside?
c#利用 TCP 协议建立连接
wpf dataGrid 实现单行某个数据变化 ui 界面随之响应
寺岗电子称修改IP简易步骤
AutoCAD - how to input angle dimensions and CAD diameter symbols greater than 180 degrees?
今日睡眠质量记录78分
NLLB-200:Meta开源新模型,可互译200种语言
Codes de non - retour à zéro inversés, codes Manchester et codes Manchester différentiels couramment utilisés pour le codage des signaux numériques
Selenium Library
华为云数据库DDS产品深度赋能
请问,PTS对数据库压测有好方案么?