小程序--本地文件转为线上文件在小程序打开
踩坑:
一开始使用腾讯文档,把本地地址转为线上地址,打开文档时报错,因为地址为html格式,当前不支持打开html格式的文件。
方案:
将文档上传至服务器,得到其在服务器的地址,文档原来是什么格式,打开之后还是什么格式
代码:
wx.downloadFile({
url: 'https://yq.damaiiot.com/yq/help/parkLiveNote.pdf', //要预览的 PDF 的地址
success: function (res) {
console.log(res);
if (res.statusCode === 200) { //成功
var Path = res.tempFilePath
console.log(Path, 'Path'); //返回的文件临时地址,用于后面打开本地预览所用
wx.openDocument({
// fileType: 'pdf', // 需要写上文件类型才能预览,不让回找系统应用打开,体验不好
filePath: Path, //要打开的文件路径
success: function (res) {
console.log('打开 PDF 成功');
}
})
}
},
fail: function (res) {
console.log(res); //失败
}
})