-
<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>