第一步直接上微信官方api
- https://developers.weixin.qq.com/miniprogram/dev/api/file/wx.openDocument.html
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/d6f932b08afc729ce036cb52f170b674.png)
- 直接这么写有问题
- 缺失文件存本地地址
- wx.openDocument 这个方法缺失打开指定文件类型
wx.downloadFile({
url: Config.BaseRequestUrl + '/common/downloadFile?id=' + id, // 要预览的PDF的地址
// header: {
// 'content-type': 'application/json'
// },
filePath: wx.env.USER_DATA_PATH + "/" + currentFileName,
success: function (res) {
if (res.statusCode === 200) { // 成功
// res.tempFilePath = res.tempFilePath.replace(".bin", ".pdf")
var Path = res.filePath // 返回的文件临时地址,用于后面打开本地预览所用
wx.openDocument({
filePath: Path, // 要打开的文件路径
fileType: suffixName,
success: function (res) {
console.log(`打开${suffixName}成功`);
},
fail: function(res) {
console.log(res)
Toast({message: '查看失败!'})
}
})
}
},
fail: function (res) {
console.log(res); // 失败
}
})
}