当前位置:网站首页>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, |
边栏推荐
- 分布式链路追踪Jaeger + 微服务Pig在Rainbond上的实践分享
- 阿尔萨斯监控平台&普罗米修斯监控平台对服务器资源的监控
- Codeforces Round #811 A~F
- AIX7.1安装Oracle11g补丁33829709(PSU+OJVM)
- 大众点评搜索相关性技术探索与实践
- leetcode:259. 较小的三数之和
- leetcode: 254. Combinations of factors
- 游戏网络 UDP+FEC+KCP
- 你一定从未看过如此通俗易懂的YOLO系列(从v1到v5)模型解读
- leetcode: 255 Verify preorder traversal sequence binary search tree
猜你喜欢

1401 - Web technology 】 【 introduction to graphical Canvas

I/O stream summary

Manacher(求解最长回文子串)

Legal education combined with VR panorama, intuitively feel and learn the spirit of the rule of law

弄懂#if #ifdef #if defined

leetcode: 253. How many meeting rooms are required at least

Redis 高可用

16. Learn MySQL Regular Expressions

实战:10 种实现延迟任务的方法,附代码!

快速整明白Redis中的字典到底是个啥
随机推荐
7 天能找到 Go 工作吗?学学 Go 数组和指针试试
PHP 图片转PDF
Go 语言快速入门指南: 变量和常量
IP第十五天笔记
苏秋贵:揭秘绿联科技用5年时间从0做到6亿,如何一枝独秀?
Taurus.MVC WebAPI 入门开发教程2:添加控制器输出Hello World。
leetcode: 254. Combinations of factors
C# 判断文件编码
leetcode:215无序数组中找第k大的元素
leetcode: 259. Smaller sum of three numbers
Http-Sumggling缓存漏洞分析
using关键字学习
leetcode: 241. Designing precedence for arithmetic expressions
RTC 场景下的屏幕共享优化实践
剑指Offer 63.股票的最大利润
宣传海报
【云原生 | 从零开始学Kubernetes】kubernetes之StatefulSet详解
IP第十六天笔记
MySQL优化学习笔记
FRED Application: Capillary Electrophoresis System