当前位置:网站首页>H5 之 文件流转base64下载
H5 之 文件流转base64下载
2022-08-04 15:19:00 【qq_45689385】
直接上代码,首先,请求时加一个 responseType 属性 本接口使用axios为例
export const gethpspfile = (data) => {
return axios.request({
url: URL_BASE + '/business/hpsp/hpysspcontroller/gethpspfile',
method: 'POST',
responseType: 'arraybuffer', // 这里加添
// responseType: 'blob',
data
});
};在就是掉请求下载文件 这里使用 try catch 是因为返回的是一个文件流 axios给拦截了
// 下载附件
async download(item) {
uni.showLoading({ title: '下载中' });
try {
let res = await gethpspfile({ RECORDID: item.RECORDID });
} catch (error) {
let type = '';
// 下载的文件类型格式
if (item.FILETYPE == 'doc') {
type = 'data:application/msword;base64,';
} else if (item.FILETYPE == 'pdf') {
type = 'data:application/pdf;base64,';
}
//arrayBufferToBase64转换为Base64
let url = type + uni.arrayBufferToBase64(error);
// 创建a标签下载
let link = document.createElement('a');
link.style.display = 'none';
link.href = url;
link.setAttribute('download', '下载文件名称');
document.body.appendChild(link);
link.click();
uni.hideLoading();
}
}base64对应的文件类型
| txt | data:text/plain;base64, |
| doc | data:application/msword;base64, |
| docx | data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64, |
| xls | data:application/vnd.ms-excel;base64, |
| xlsx | data:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;base64, |
| data:application/pdf;base64, | |
| pptx | data:application/vnd.openxmlformats-officedocument.presentationml.presentation;base64, |
| ppt | data:application/vnd.ms-powerpoint;base64, |
| png | data:image/png;base64, |
| jpg | data:image/jpeg;base64, |
| gif | data:image/gif;base64, |
| svg | data:image/svg+xml;base64, |
| ico | data:image/x-icon;base64, |
| bmp | data:image/bmp;base64, |
边栏推荐
猜你喜欢
随机推荐
leetcode: 255 Verify preorder traversal sequence binary search tree
IP第十七天笔记
Next -19- 开启fancybox查看图片大图
技术分享| 小程序实现音视频通话
RSA306B,500,600系列API接口代码
Next -21- 添加相册系列 - 1- 框架设置
OGG判断mgr状态并自动启动脚本
leetcode:215无序数组中找第k大的元素
解决dataset.mnist无法加载进去的情况
明明加了唯一索引,为什么还是产生重复数据?
Nuget 通过 dotnet 命令行发布
分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
24、shell编程-流程控制
Resharper 如何把类里的类移动到其他文件
QT笔记——QUuid了解
2022年7月国产数据库大事记-墨天轮
基于 Next.js实现在线Excel
RepVGG学习笔记
eNSP-小型网络拓扑(DNS、DHCP、网站服务器、无线路由器)
学 Go,最常用的技能是什么?打日志








