uni-app实现h5、app和微信小程序三端pdf文件下载和预览功能
以下代码兼容三端,app,h5,微信小程序,经过个人测试
手机端有两种方法,使用renderjs或者uniapp的api
两者的区别
- 使用renderjs的写法,会提示用户是否下载文件,下载完成后用户需要手动点击下载的文件,才会打开文件
- 使用uniapp的api则可以直接下载并直接预览,不需要用户操作
- 根据场景需求进行选择即可
-
-
<template>
-
<div>
-
<!-- #ifdef APP-PLUS -->
-
<button @click="test.exportPDF">预览和下载pdf(renderjs)</button>
-
<button @click="exportPDF">预览和下载pdf(uniapp api)</button>
-
<!-- #endif -->
-
<!-- #ifndef APP-PLUS -->
-
<button @click="exportPDF">预览和下载pdf</button>
-
<!-- #endif -->
-
</div>
-
</template>
-
-
<!-- #ifdef APP-PLUS -->
-
<script module="test" lang="renderjs">
-
export default {
-
methods: {
-
exportPDF() {
-
const Url = "https://vkceyugu.cdn.bspapp.com/VKCEYUGU-7da443bc-353a-4224-ab27-b98917aa6c66/89d1d612-734a-4219-9110-0b21fb004d5f.pdf"
-
const a = document.createElement("a")
-
a.href = Url
-
a.download = "download"
-
a.click()
-
}
-
}
-
}
-
</script>
-
<!-- #endif -->
-
-
<script>
-
export default {
-
methods: {
-
exportPDF() {
-
// #ifdef H5
-
window.open(
-
"https://vkceyugu.cdn.bspapp.com/VKCEYUGU-7da443bc-353a-4224-ab27-b98917aa6c66/89d1d612-734a-4219-9110-0b21fb004d5f.pdf"
-
)
-
// #endif
-
-
// 微信下载文件需要在微信公众平台>开发>开发管理>服务器域名>downloadFile合法域名>配置白名单域名
-
// #ifdef MP-WEIXIN
-
uni.downloadFile({
-
url:
-
"https://vkceyugu.cdn.bspapp.com/VKCEYUGU-7da443bc-353a-4224-ab27-b98917aa6c66/89d1d612-734a-4219-9110-0b21fb004d5f.pdf",
-
success: res => {
-
console.log(res)
-
if (res.statusCode === 200) {
-
// 预览pdf文件
-
uni.openDocument({
-
filePath: res.tempFilePath,
-
showMenu: true, // 右上角菜单,可以进行分享保存pdf
-
success: function(file) {
-
console.log("file-success", file)
-
}
-
})
-
}
-
}
-
})
-
// #endif
-
-
// #ifdef APP-PLUS
-
uni.downloadFile({
-
url:
-
"https://vkceyugu.cdn.bspapp.com/VKCEYUGU-7da443bc-353a-4224-ab27-b98917aa6c66/89d1d612-734a-4219-9110-0b21fb004d5f.pdf",
-
success: res => {
-
console.log(res)
-
if (res.statusCode === 200) {
-
// 保存pdf文件至手机,一般安卓端存储路径为:手机存储/dcim/camera文件夹下
-
uni.saveImageToPhotosAlbum({
-
filePath: res.tempFilePath,
-
success: function() {
-
uni.showToast({
-
title: "文件已保存至/DCIM/CAMERA文件夹下",
-
icon: "none"
-
})
-
setTimeout(function() {
-
// 预览pdf文件
-
uni.openDocument({
-
filePath: res.tempFilePath,
-
showMenu: true,
-
success: function(file) {
-
console.log("file-success", file)
-
}
-
})
-
}, 1500)
-
},
-
fail: function() {
-
uni.showToast({
-
title: "保存失败,请稍后重试!",
-
icon: "none"
-
})
-
}
-
})
-
}
-
}
-
})
-
// #endif
-
}
-
}
-
}
-
</script>
-
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfihcia
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01