背景:开发了一个微信小程序 ,列表页面会有3个pdf下载按钮,实现小程序pdf下载到本地
1.pdf地址不在本地服务器上面
下载文件之后生成一个临时地址,使用openDocument打开,利用页面右上角...将pdf保存到本地
downFile(event) {
let url = event.currentTarget.dataset.url
wx.downloadFile({
url: url,
success: function (res) {
const filePath = res.tempFilePath
wx.showToast({ title: '下载成功', icon: 'none' })
wx.openDocument({
filePath: filePath,
fileType: 'pdf',
showMenu: true,
success: function (res) {
console.log('预览成功')
},
fail: err => {
wx.showToast({ title: '预览失败', icon: 'none' })
}
})
}
})
},
2.pdf地址在服务器上面
用户可以直接通过web-view组件在线查看pdf文件,也可以点击相应的下载按钮将文件下载到本地。
<web-view src="https://www....."></web-view>