微信原生小程序下载excel、pdf文件分享到微信
·
主要用到了微信原生小程序的两个api,wx.downloadFile和wx.shareFileMessage
exportQuoteOrder是点击导出时调用的方法。调用导出接口,后端会返回文件的地址url,包含了文件名信息等
exportQuoteOrder() {
app.requestPost(this, {
id: this.data.exportId
}, exportSaleApi, res => {
if (res.data.code === 200) {
this.downloadAndShare(res.data.data);
}
});
},
downloadAndShare(path) {
let that = this;
wx.showLoading({
title: '下载中...'
});
// 下载地址 api是服务器请求地址
const filePath = `${api}/${path}`;
// 文件名称
let fName = path.split("/")[1];
console.log(filePath, fName, '------文件下载地址')
wx.downloadFile({
url: filePath, // 文件本身的url
success(res) {
if (res.statusCode === 200) {
wx.hideLoading();
// 下载成功,处理文件 分享或打开
wx.shareFileMessage({
filePath: res.tempFilePath,
fileName: fName,
success: () => {},
});
}
},
fail(err) {
console.log('下载失败', err);
wx.showToast({
title: '下载失败',
icon: 'none'
});
}
});
}



下载和分享api还有很多参数,具体的可以在官网查看
更多推荐


所有评论(0)