base64转blob vue
时间: 2023-10-04 07:06:04 浏览: 72
在 Vue 中将 base64 转换为 Blob 的方法如下:
1. 先将 base64 字符串转为 Uint8Array 类型:
```javascript
let base64 = 'YOUR_BASE64_STRING';
let binaryString = window.atob(base64);
let binaryLen = binaryString.length;
let bytes = new Uint8Array(binaryLen);
for (let i = 0; i < binaryLen; i++) {
let ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
```
2. 然后将 Uint8Array 转为 Blob 对象:
```javascript
let blob = new Blob([bytes], { type: 'image/jpeg' });
```
完整的代码如下:
```javascript
export default {
methods: {
base64ToBlob(base64) {
let binaryString = window.atob(base64);
let binaryLen = binaryString.length;
let bytes = new Uint8Array(binaryLen);
for (let i = 0; i < binaryLen; i++) {
let ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
let blob = new Blob([bytes], { type: 'image/jpeg' });
return blob;
}
}
}
```
使用方法:
```javascript
let base64String = 'YOUR_BASE64_STRING';
let blob = this.base64ToBlob(base64String);
```
注意:上面的代码中,type 参数需要根据实际情况进行修改,例如图片类型为 jpeg,则 type 为 image/jpeg。
阅读全文