vue base64转图片
时间: 2023-08-08 08:09:13 浏览: 94
vue+前端,进行人脸识别,图片上传、可转换base64,以及文件上传
在Vue中将Base64字符串转换为图片可以使用以下方法:
1. 将Base64字符串转换为Blob对象:
```javascript
let byteCharacters = atob(base64String);
let byteArrays = [];
for (let i = 0; i < byteCharacters.length; i++) {
byteArrays.push(byteCharacters.charCodeAt(i));
}
let blob = new Blob([new Uint8Array(byteArrays)], {type: 'image/png'});
```
2. 将Blob对象转换为URL:
```javascript
let imageUrl = URL.createObjectURL(blob);
```
3. 在Vue中使用图片URL:
```html
<template>
<div>
<img :src="imageUrl" />
</div>
</template>
<script>
export default {
data() {
return {
imageUrl: null
};
},
mounted() {
let byteCharacters = atob(base64String);
let byteArrays = [];
for (let i = 0; i < byteCharacters.length; i++) {
byteArrays.push(byteCharacters.charCodeAt(i));
}
let blob = new Blob([new Uint8Array(byteArrays)], {type: 'image/png'});
this.imageUrl = URL.createObjectURL(blob);
}
};
</script>
```
阅读全文