//文件下载
export function download(fileUrl,data,name) {//地址,参数,文件名称
wx.request({
url: fileUrl, // 你的文件下载地址
method: 'POST',
header: {
Authorization: uni.getStorageSync("token")//后台的tokrn
},
data: data,
responseType: 'arraybuffer', // 设置响应类型为arraybuffer以接收二进制数据
success: (res) => {
const filePath = wx.env.USER_DATA_PATH + name?name:'/downloadedfile.xlsx'; // 文件保存路径
const fileWriter = wx.getFileSystemManager();
fileWriter.writeFile({
filePath: filePath,
data: res.data,
success: (writeRes) => {
console.log('文件保存成功', writeRes);
uni.saveFile({
tempFilePath: filePath,
success: (res1) => {
var saveFilePath = res1.savedFilePath;
uni.showModal({
title: "提示",
content: "文件已保存:" + saveFilePath,
cancelText: "我知道了",
confirmText: "打开文件",
success: (res2) => {
if (res2.confirm) {
uni.openDocument({
filePath: saveFilePath,
success: (res) => {
console.log("打开文档成功", JSON.stringify(res))
}
})
}
}
})
}
})
},
fail: (writeErr) => {
console.error('文件保存失败', writeErr);
}
});
},
fail: (err) => {
console.error('文件下载失败', err);
}
});
}
uniapp 微信小程序文件下载
最新推荐文章于 2024-08-08 17:14:10 发布