当前位置:网站首页>van-uploader uploads images, and cannot preview the image using base64 echo
van-uploader uploads images, and cannot preview the image using base64 echo
2022-07-31 07:40:00 【puppet running】
1.今天在开发过程中,遇到了使用vant上传组件,The echo cannot be previewed,在搜索后,解决方法如下:
- html:
<van-uploader v-model="fileList"
accept='.jpg,.jpeg,.png'
:before-read="beforeRead"
:after-read="afterRead"
:max-size="10000 * 1024"
@oversize="onOversize"
:before-delete="beforeDel" />
- js
fileNameList.split(',').forEach(v => {
// Iterate over the filenames returned by the backend
this.$http.secondParty.getImageByName({
fileName: v }).then(k => {
// filename request tobase64格式文件
// 图片base64转url
let img = 'data:image/jpeg;base64,' + k.data
let imgurl = this.base64ImgtoFile(img)
console.log('imgurl===', imgurl)
let url =
window.webkitURL.createObjectURL(imgurl) ||
window.URL.createObjectURL(imgurl)
// 创建图片地址
this.fileList.push({
content: 'data:image/jpeg;base64,' + k.data,
File: imgurl,
name: v,
status: 'done',
message: '上传中',
isImage: true,
url
})
})
})
- base64转文件
base64ImgtoFile(dataurl, filename = 'file') {
const arr = dataurl.split(',')
const mime = arr[0].match(/:(.*?);/)[1]
const suffix = mime.split('/')[1]
const bstr = atob(arr[1])
let n = bstr.length
const u8arr = new Uint8Array(n)
while (n--) {
u8arr[n] = bstr.charCodeAt(n)
}
return new File([u8arr], `${
filename}.${
suffix}`, {
type: mime
})
},
边栏推荐
猜你喜欢
2022.07.29_每日一题
链表实现及任务调度
LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】
【Go报错】go go.mod file not found in current directory or any parent directory 错误解决
从入门到一位合格的爬虫师,这几点很重要
2022.07.24_每日一题
Titanic 预测问题
2022.07.12_每日一题
金融租赁业务
Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
随机推荐
LeetCode刷题——摆动序列#376#Medium
tidyverse笔记——dplyr包
nohup原理
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
DAY18: XSS vulnerability
线程中断方法
How to set the computer password?How to add "safety lock" to your computer
2022.7.29 数组
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
PCB抄板
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
金融租赁业务
How to choose a suitable UI component library in uni-app
【Star项目】小帽飞机大战(七)
【微服务】Nacos集群搭建以及加载文件配置
Titanic 预测问题
关于求反三角函数的三角函数值
Leetcode952. 按公因数计算最大组件大小
【Star项目】小帽飞机大战(八)
Bulk free text translation