重点:一定要在后台提前设置好downloadFile 服务器域名(也就是你要访问的文件地地址),不然无法下载,我当时就遇到这个问题了,还找了半天原因。
![](https://img.haomeiwen.com/i18014667/6d7d345f143a8d2d.png)
image.png
onHelpCenter(){
wx.showLoading({
title: '加载中',
})
const fileExtName = 'pdf';
const randfile = new Date().getTime() + fileExtName;
const newPath = `${wx.env.USER_DATA_PATH}/${randfile}`;
wx.downloadFile({
url: 'xxx', // 本地服务器上pdf文件的地址
filePath: newPath,
success (res) {
// 只要服务器有响应数据,就会把响应内容写入文件并进入 success 回调,业务需要自行判断是否下载到了想要的内容
if (res.statusCode === 200) {
console.log(res)
let data = res.filePath
wx.openDocument({
filePath: newPath,
showMenu: true,
fileType: 'pdf',
success:function(res){
wx.hideLoading()
}
})
}
},
fail:function(res){
console.log(res)
}
})
}
网友评论