当前位置:网站首页>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, |
边栏推荐
- 图解 SQL,这也太形象了吧!
- leetcode:254. 因子的组合
- 365天挑战LeetCode1000题——Day 049 非递增顺序的最小子序列 贪心
- IP报文头解析
- 《分布式云最佳实践》分论坛,8月11日深圳见
- Codeforces Round #811 A~F
- IP第十七天笔记
- Codeforces Round #811 A~F
- SublimeText 粘贴图片保存到本地
- 【Today in History】August 4: First female Turing Award winner; NVIDIA acquires MediaQ; first Cybersecurity Challenge completed
猜你喜欢
FRED Application: Capillary Electrophoresis System
Cisco - Small Network Topology (DNS, DHCP, Web Server, Wireless Router)
Why, when you added a unique index or create duplicate data?
remote: Check Access Error, please check your access right or username and password!fatal: Authenti
Hangzhou Electric School Competition (Counter Attack Index)
CloudCompare&PCL 点云按网格划分(点云分幅)
我在羊毛和二手群里报复性消费
FTP协议抓包-工具wireshark与filezilla
洛谷题解P1028 数的计算
【北亚数据恢复】IBM System Storage存储lvm信息丢失,卷访问不了的数据恢复方案
随机推荐
Sublime Text 好用的插件
C# SolidWorks二次开发---工程图简单版标注孔信息
Roslyn 节点的 Span 和 FullSpan 有什么区别
剑指Offer 63.股票的最大利润
7 天能找到 Go 工作吗?学学 Go 数组和指针试试
Next -18- 添加代码复制按钮
你一定从未看过如此通俗易懂的YOLO系列(从v1到v5)模型解读
Hangzhou Electric School Competition (Counter Attack Index)
leetcode:215无序数组中找第k大的元素
Go 语言快速入门指南: 变量和常量
Android Sqlite3 basic commands
直播回放含 PPT 下载|基于 Flink & DeepRec 构建 Online Deep Learning
Oracle database user creation, restart, import and export
C端折戟,转战B端,联想的元宇宙梦能成吗?
OAID是什么
QT笔记——QUuid了解
leetcode: 250. Count subtrees of equal value
Resharper 如何把类里的类移动到其他文件
Unity AR阴影投射透明地面 仅渲染模型实时阴影 Shader实现
指数族分布与最大熵