当前位置:网站首页>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, |
边栏推荐
- IP第十七天笔记
- Hangzhou Electric School Competition (Counter Attack Index)
- 【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
- AIX7.1安装Oracle11g补丁33829709(PSU+OJVM)
- NUS颜水成等发布首篇《深度长尾学习》综述
- 微软表示将向内部网络安全专家共享数据 为企业提供更安全保护
- 我在羊毛和二手群里报复性消费
- 从-99打造Sentinel高可用集群限流中间件
- 李沐的深度学习笔记来了!
- 365天挑战LeetCode1000题——Day 049 非递增顺序的最小子序列 贪心
猜你喜欢
leetcode: 251. Expanding 2D Vectors
remote: Check Access Error, please check your access right or username and password!fatal: Authenti
1403. 非递增顺序的最小子序列
从-99打造Sentinel高可用集群限流中间件
Byte、Short、Integer、Long内部缓存类的对比与源码分析
leetcode: 259. Smaller sum of three numbers
LeetCode_模拟_中等_498.对角线遍历
(2022杭电多校五)C - Slipper (dijkstra+虚拟结点)
MySQL优化学习笔记
Semaphore 基本原理
随机推荐
C# 局部函数与事件
OAID是什么
leetcode:215无序数组中找第k大的元素
直播系统开发——直播间架构的设计及难点分析
【已解决】allure无法生成json文件和AttributeError: module ‘allure‘ has no attribute ‘severity_level‘
【Harmony OS】【FAQ】鸿蒙问题合集2
技术分享| 小程序实现音视频通话
我爱七夕哈哈哈
动态数组底层是如何实现的
How to fall in love with a programmer
弄懂#if #ifdef #if defined
快速整明白Redis中的字典到底是个啥
CloudCompare&PCL 点云按网格划分(点云分幅)
This week to discuss the user experience: Daedalus Nemo to join Ambire, explore the encryption of the ocean
2022杭电多校3
AOSP built-in APP franchise rights white list
基本介绍PLSQL
leetcode: 255 Verify preorder traversal sequence binary search tree
uni-app 从零开始-生命周期(二)
PTA 6-2 多项式求值