当前位置:网站首页>van-uploader上传图片,使用base64回显无法预览的问题
van-uploader上传图片,使用base64回显无法预览的问题
2022-07-31 06:10:00 【布偶乱跑】
1.今天在开发过程中,遇到了使用vant上传组件,回显无法预览的问题,在搜索后,解决方法如下:
- 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 => {
// 遍历后端返回的文件名
this.$http.secondParty.getImageByName({
fileName: v }).then(k => {
// 文件名请求到base64格式文件
// 图片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
})
},
边栏推荐
猜你喜欢

完美指南|如何使用 ODBC 进行无代理 Oracle 数据库监控?

Difficulty comparison between high concurrency and multithreading (easy to confuse)

360推送-360推送工具-360批量推送工具

Web浏览器工作流程解析

基于交替迭代法的交直流混合系统潮流计算matlab程序iEEE9节点系统算例

iOS大厂面试查漏补缺

Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model

【解决】npm ERR A complete log of this run can be found in npm ERR

Bulk free text translation

搭建zabbix监控及邮件报警(超详细教学)
随机推荐
Log4net 思维导图
2022.07.29_每日一题
Database Principles Homework 3 — JMU
SQL Server Datetime2数据类型
从 Google 离职,前Go 语言负责人跳槽小公司
Analysis of the principle and implementation of waterfall flow layout
codec2 BlockPool:不可读库
Obtaining server and client information
Conditional statements of shell (test, if, case)
LeetCode:952. 按公因数计算最大组件大小【欧拉筛 + 并查集】
Zabbix6.2惊喜发布!特别优化中大型环境部署的性能!
Gradle剔除依赖演示
2022.07.15_每日一题
小实战项目之——吃货联盟订餐系统
Zero-Shot Learning & Domain-aware Visual Bias Eliminating for Generalized Zero-Shot Learning
Exam Questions Previous True Questions Wrong Bills [The Fourth Session] [Provincial Competition] [Group B]
mysql索引失效的常见9种原因详解
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
文件 - 03 下载文件:根据文件id获取下载链接
leetcode 406. Queue Reconstruction by Height