有关后端返回文件流或base64文件,前端如何实现图片展示和下载
创始人
2025-01-16 12:35:01
0
后端返回的文件流,前端怎么把图片显示出来

 1.多年前端开发经验的我目前用的最多的是:直接将请求地址写在src里面,如:

 2.由于后台加上鉴权后,需要每个接口在header里面加上token,上面的做法就行不通了,需要发送请求获得文件流

const file_data = res.data // 后端返回的文件流   const blob = new Blob([file_data]) const url = window.URL.createObjectURL(blob) // image中src的链接

3.后台返回base64文件

srcurl= 'data:application/octet-stream;base64,' + res
js下载后端返回的文件

1.后端返回文件流形式,前端下载

// res 为后端返回的响应对象   const file_data = res.data // 后端返回的文件流   const blob = new Blob([file_data]) const url = window.URL.createObjectURL(blob) // 创建下载的链接 const file_name = decodeURI(res.headers['content-disposition'].replace('attachment;filename=', '')) // 需要后端暴露请求头'Content-Disposition' console.log(file_name) // 从请求头获取文件名 const downloadElement = document.createElement('a') downloadElement.style.display = 'none' downloadElement.href = url downloadElement.download = file_name // 下载后文件名 document.body.appendChild(downloadElement) downloadElement.click() document.body.removeChild(downloadElement) // 下载完成移除元素 window.URL.revokeObjectURL(url) // 释放掉blob对象   注:如果下载文件变大打不开,可以设置 responseType: 'arraybuffer'  async reqestApi(param) {     const res = await axios.get('exportExeclData', { params: param, responseType: 'arraybuffer' })     }

2.后端返回base64格式,前端下载

// res 后端返回的文件base64   const link = document.createElement('a') link.href = 'data:application/octet-stream;base64,' + res link.download = '下载的文件名' link.click()

相关内容

热门资讯

大纲f辅助!wpk俱乐部有没有... 大纲f辅助!wpk俱乐部有没有辅助,wepoker祈福有用吗,必备教程(有挂详情)1、该软件可以轻松...
方针f辅助!红龙poker辅助... 方针f辅助!红龙poker辅助器免费观看,wepoker插件下载,解迷教程(有人有挂)1、许多玩家不...
手册f辅助!大菠萝789辅助器... 手册f辅助!大菠萝789辅助器下载,wepoker破解器,专业教程(有挂细节)1、每一步都需要思考,...
策略f辅助!wepoker怎么... 策略f辅助!wepoker怎么拿到好牌,aapoker透视插件,详细教程(有挂攻略)1)wepoke...
法子f辅助!wepoker辅助... 法子f辅助!wepoker辅助器激活码,聚星ai辅助工具下载,详细教程(竟然有挂)1、玩家可以在聚星...
手筋f辅助!xpoker怎么作... 手筋f辅助!xpoker怎么作弊,wejoker手机版透视脚本,详情教程(有挂方针)1、wejoke...
讲义f辅助!wepokerpl... 讲义f辅助!wepokerplus开挂,wepoker辅助工具,普及教程(揭秘有挂)1、不需要AI权...
学习f辅助!pokemomo辅... 学习f辅助!pokemomo辅助软件,newpoker脚本,分享教程(真的有挂)1、每一步都需要思考...
经验f辅助!aapoker辅助... 经验f辅助!aapoker辅助工具安全吗,sohoopoker辅助,有挂教程(有挂解密)暗藏猫腻,小...
模块f辅助!wepoker怎么... 模块f辅助!wepoker怎么挂飞机,hhpkoer辅助器视频,有挂教程(发现有挂)1、实时hhpk...