以下代码兼容三端,app,h5,微信小程序,经过个人测试 手机端有两种方法,使用renderjs或者uniapp的api 两者的区别 使用renderjs的写法,会提示用户是否下载文
以下代码兼容三端,app,h5,微信小程序,经过个人测试
手机端有两种方法,使用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>
总结
到此这篇关于uniapp实现h5、app与微信小程序三端pdf文件下载和预览功能的文章就介绍到这了,更多相关uniapp实现pdf文件下载预览内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: uniapp实现h5、app与微信小程序三端pdf文件下载和预览功能
本文链接: https://www.lsjlt.com/news/174653.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-12
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
2023-05-20
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0