wx.openDocument 官方文档里写着需要一个叫 filePath 的参数,表示文件路径,需要通过 downFile 获得。
所以,要使用 openDocument 得先使用 downloadFile .
其实 downloadFile 不是下载文件到本地,只是建立了一个临时文件,只在小程序的当前次运行中起效,而 saveFile 才是永久保存文件在本地。
再说 openDocument 为什么要非得要 downloadFile 来搞个临时文件呢,因为 openDocument 接收的 filePath 路径,就是要本地临时路径,换成相对路径、本地路径、甚至全路径,都无法实现。
downloadFile 返回的 tempFilePath 就是本地临时路径。
以下是代码:
helpDocument:function(){
var url = 'http://baidu.com/upload/helpDocument.pdf' //要预览的文件路径
wx.downloadFile({
url: url,
success: function (res) {
console.log("成功下载后返回参数==");
console.log(res);
var filePath = res.tempFilePath
wx.openDocument({
filePath: