问题
项目需要用uniapp来开发微信小程序,其中,有一个功能是预览pdf, 后台以链接形式返回pdf地址,一开始使用web-view做链接跳转,但是出现ios文件可以正常打开,android打不开的情况,本来想用pdf.js做,后来发现微信自带的有一个API可以打开文档,于是就试了下。
解决方法
wx.downloadFile({
url: 'https://xxxx.xx/example.pdf',
success: function(res) {
const filePath = res.tempFilePath;
wx.openDocument({
filePath: filePath,
success: function(res) {
console.log('打开文档成功');
}
});
}
});
新问题
这样做之后,是没有问题的,安卓、ios都可以正常打开文件。但是,因为我是从A页面点击,跳转到B页面,把链接带到B页面,B页面嵌入web-view, 所以安卓在点击左上角返回的时候就会出现返回到空白页的情况。但是IOS就不会出现,我继续改!
新解决方法
我就在A页面判断一下手机的系统,如果是安卓就不用跳到B页面,直接用微信小程序API打开,如果是ios,就跳到B页面,用web-view打开。
uni.getSystemInfo({
success: res => {
console.log(res.platform);
if (res.platform === 'android') {
wx.downloadFile({
url: this.fileUrl,
success: function(res) {
const filePath = res.tempFilePath;
wx.openDocument({
filePath: filePath,
success: function(res) {
console.log('打开文档成功');
}
});
}
});
} else {
// this.url = this.fileUrl;
uni.navigateTo({
url: '../webView/webView?DataPath=' + val.DataPath
});
}
}
});
参考链接: https://blog.csdn.net/qq_41575586/article/details/103903735.