小程序批量下载压缩包(zip文件)
# wxml
>
<view class="search_btn" catchtap="batchDownload">批量下载</view>
```js
// 批量下载
batchDownload(e){
const {multipleCheckId,orderList}=this.data;
const ids=multipleCheckId.map(item=>item.id)
const Token = wx.getStorageSync("token");
const first_token = wx.getStorageSync("first_token");
const second_token = wx.getStorageSync("second_token");
const that=this;
if(!ids.length||ids.length>20){
wx.showToast({
title: ids.length>20?"选择的环境样本不能超过20个":"请选择可下载报告的环境样本",
icon: "none",
duration: 2000,
});
return;
}
wx.showLoading({
title: "下载中",
});
if(ids.length===1){ // 单个下载 pdf文件
wx.request({
url: API_BASE_URL + `environment/samples/report`,
method: "POST",
header: {
"Content-Type": "application/json",
token: `${Token}`,
"first-token": first_token,
"second-token":second_token,
},
data: {ids:ids},
responseType: "arraybuffer",
success(res) {
wx.hideLoading();
var fileManager = wx.getFileSystemManager();
var FilePath =
wx.env.USER_DATA_PATH + "/" +"环境"+
"-" +
Math.floor(Math.random() * 1000) +
"批量下载报告.pdf";
fileManager.writeFile({
data: res.data,
filePath: FilePath,
encoding: "binary",
success(res) {
// 编码格式
wx.openDocument({
//成功之后直接打开
filePath: FilePath,
showMenu: true,
fileType: "pdf",
success: (result) => {
// 打开文档成功
wx.hideLoading({
success: (res) => {},
});
const newList = orderList.map(item=>{
const newItem = {...item};
if(newItem.checked==true){
newItem.checked=false;
}
return newItem
})
that.setData({orderList:newList,multipleCheckId:[]})
},
fail: (err) => {
// 打开文档失败
wx.hideLoading({
success: (res) => {},
});
},
});
},
fail: (res) => {
wx.hideLoading({
success: (res) => {},
});
wx.showToast({
title: "下载失败!",
icon: "none",
duration: 2000, //停留时间
});
},
});
},
});
}else{ // 批量下载压缩包文件
const fileType=that.data.fileType
const now = new Date();
let year = now.getFullYear();
let month =
(now.getMonth() + 1) < 10 ? "0" + (now.getMonth() + 1) : now.getMonth() + 1;
let day = now.getDate() < 10 ? "0" + now.getDate() : now.getDate();
let hour = now.getHours()< 10 ? "0" + now.getHours() : now.getHours(); // 时
let minutes = now.getMinutes()< 10 ? "0" + now.getMinutes() : now.getMinutes(); // 分
let seconds = now.getSeconds()< 10 ? "0" + now.getSeconds() : now.getSeconds() //秒
let today =`${year}${month}${day}${hour}${minutes}${seconds}`;
wx.request({
url: API_BASE_URL + `environment/samples/report`,
method: "POST",
header: {
"Content-Type": "application/json",
token: `${Token}`,
"first-token": first_token,
"second-token":second_token,
},
data: {ids:ids},
responseType: "arraybuffer",
success(res) {
var fileManager = wx.getFileSystemManager();
var FilePath =
wx.env.USER_DATA_PATH + "/" +"环境"+
"-" +"批量下载报告"+"-"+today+
".zip";
fileManager.writeFile({
data: res.data,
filePath: FilePath,
encoding: "binary",
success(res) {
wx.hideLoading();
that.setData({
FilePathUrl:FilePath
})
// that.uuzip(FilePath, fileType) // 解压
// 编码格式
wx.openDocument({
//成功之后直接打开
filePath: FilePath,
showMenu: true,
fileType: "zip",
success: (result) => {
const newList = orderList.map(item=>{
const newItem = {...item};
if(newItem.checked==true){
newItem.checked=false;
}
return newItem
})
that.setData({orderList:newList,multipleCheckId:[]})
// 打开文档成功
wx.hideLoading({
success: (res) => {},
});
},
fail: (err) => {
// 打开文档失败
wx.hideLoading({
success: (res) => {},
});
},
});
},
fail: (res) => {
wx.hideLoading({
success: (res) => {},
});
wx.showToast({
title: "导出失败!",
icon: "none",
duration: 2000, //停留时间
});
},
});
},
});
}
},
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了