有关后端返回文件流或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()

相关内容

热门资讯

aapoker透视软件!hhp... aapoker透视软件!hhpoker外挂靠谱吗,红龙poker有辅助吗(详细辅助必赢教程)是一款可...
透视讲解“hhpoker透视脚... 透视讲解“hhpoker透视脚本视频”确实存在有挂(辅助挂)详细辅助专业教程;一、AI软件牌型概率发...
透视讲解!aapoker脚本怎... 透视讲解!aapoker脚本怎么用,德州局透视脚本免费版下载手机版(详细辅助AI教程);1.德州局透...
透视透视!hhpoker辅助器... 透视透视!hhpoker辅助器,潮友会app下载安卓(果然有挂)1、下载好潮友会app下载安卓辅助软...
aapoker辅助是真的吗!h... aapoker辅助是真的吗!hhpoker外挂靠谱吗,wpk透视表(详细辅助曝光教程)是一款可以让一...
透视透视“wpk辅助插件叫什么... 透视透视“wpk辅助插件叫什么”原来是真的有挂(辅助挂)详细辅助AI教程;小薇(透视辅助)致您一封信...
透视了解!智星菠萝辅助怎么买,... 透视了解!智星菠萝辅助怎么买,aa poker透视软件(详细辅助安装教程)关于aa poker透视软...
透视最新!hhpoker德州机... 透视最新!hhpoker德州机器人,微信随意玩辅助器有开挂吗(一贯是有挂)1、起透看视 微信随意玩辅...
aapoker辅助功能!aap... aapoker辅助功能!aapoker怎么提高中牌率,德普之星辅助功能如何打开(详细辅助普及教程)1...
透视工具!hhpoker万能辅... 透视工具!hhpoker万能辅助器,wejoker透视方法(详细辅助2025新版技巧)1、玩家可以在...