接口返回数据流预览方式:
// 导出验货报告
async handleExport() {
wx.showLoading({
title: "加载中",
});
const { accessToken: token } = getToken();
wx.request({
url: `${BASE_URL}/order/inspectionOrder/export`,
data: { inspectionOrderId: this.data.detail.inspectionOrderId },
method: "get",
timeout: 60 * 1000,
header: {
"Content-type": "application/x-www-form-urlencoded",
tokenId: token,
systemCode: "erp",
Authorization: `Bearer ${token}`,
},
responseType: "arraybuffer",
success: (res) => {
console.log('res', res)
const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
const filePath = `${wx.env.USER_DATA_PATH}/${this.data.detail.number}.pdf`;
fs.writeFile({
filePath, // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
data: res.data,
encoding: "binary", //二进制流文件必须是 binary
success() {
wx.openDocument({
// 打开文档
filePath, //拿上面存入的文件路径
showMenu: true, // 显示右上角菜单
fileType: "pdf",
success: function () {
setTimeout(() => {
wx.hideLoading();
}, 500);
},
complete() {
},
});
},
});
},
complete: function () {
wx.hideLoading();
},
});
},
接口返回url预览方式
uni.downloadFile({
url: fileUrl,
success: function (res) {
if (res.statusCode === 200) {
uni.openDocument({
filePath: res.tempFilePath,
fileType: fileType,
showMenu: true,
success: function (res) {
console.log('打开文档成功')
},
fail: function (err) {
console.error('打开文档失败', err)
uni.showToast({
title: '打开文档失败',
icon: 'none'
})
}
})
}
},
fail: function (err) {
console.error('下载文件失败', err)
uni.showToast({
title: '下载文件失败',
icon: 'none'
})
},
complete: function () {
uni.hideLoading()
}
})