file转base64 vue
时间: 2023-09-05 09:12:04 浏览: 277
在Vue中将文件转换为base64的方法可以使用以下代码:
```javascript
getBase64(file) {
return new Promise((resolve, reject) => {
let reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function() {
resolve(reader.result);
};
reader.onerror = function(error) {
reject(error);
};
});
}
```
你可以在Vue组件中调用这个方法来将文件转换为base64:
```javascript
this.getBase64(file).then(res => {
this.base64Data = res;
});
```
这样,你就可以将文件转换为base64并将结果存储在Vue组件的数据属性中了。
#### 引用[.reference_title]
- *1* [vue+上传,将拿到的file转成base64地址](https://blog.csdn.net/qq_41619796/article/details/109644538)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [vue 图片file转Base64编码格式](https://blog.csdn.net/Zmz0202/article/details/129553850)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [vue中base64和file文件类型的互相转换](https://blog.csdn.net/qq_39285178/article/details/118159284)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
阅读全文