uniapp實(shí)現(xiàn)h5、app與微信小程序三端pdf文件下載和預(yù)覽功能
以下代碼兼容三端,app,h5,微信小程序,經(jīng)過個人測試
手機(jī)端有兩種方法,使用renderjs或者uniapp的api
兩者的區(qū)別
- 使用renderjs的寫法,會提示用戶是否下載文件,下載完成后用戶需要手動點(diǎn)擊下載的文件,才會打開文件
- 使用uniapp的api則可以直接下載并直接預(yù)覽,不需要用戶操作
- 根據(jù)場景需求進(jìn)行選擇即可
<template> <div> <!-- #ifdef APP-PLUS --> <button @click="test.exportPDF">預(yù)覽和下載pdf(renderjs)</button> <button @click="exportPDF">預(yù)覽和下載pdf(uniapp api)</button> <!-- #endif --> <!-- #ifndef APP-PLUS --> <button @click="exportPDF">預(yù)覽和下載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 // 微信下載文件需要在微信公眾平臺>開發(fā)>開發(fā)管理>服務(wù)器域名>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) { // 預(yù)覽pdf文件 uni.openDocument({ filePath: res.tempFilePath, showMenu: true, // 右上角菜單,可以進(jìn)行分享保存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文件至手機(jī),一般安卓端存儲路徑為:手機(jī)存儲/dcim/camera文件夾下 uni.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function() { uni.showToast({ title: "文件已保存至/DCIM/CAMERA文件夾下", icon: "none" }) setTimeout(function() { // 預(yù)覽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>
總結(jié)
到此這篇關(guān)于uniapp實(shí)現(xiàn)h5、app與微信小程序三端pdf文件下載和預(yù)覽功能的文章就介紹到這了,更多相關(guān)uniapp實(shí)現(xiàn)pdf文件下載預(yù)覽內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JS實(shí)現(xiàn)隱藏同級元素后只顯示JS文件內(nèi)容的方法
這篇文章主要介紹了JS實(shí)現(xiàn)隱藏同級元素后只顯示JS文件內(nèi)容的方法,可實(shí)現(xiàn)將與js文件的同級元素全部隱藏,只顯示js文件內(nèi)容的功能,涉及javascript針對頁面元素的遍歷與屬性修改相關(guān)技巧,需要的朋友可以參考下2016-09-09ionic cordova一次上傳多張圖片(類似input file提交表單)的實(shí)現(xiàn)方法
這篇文章主要介紹了ionic cordova一次上傳多張圖片(類似input file提交表單)的實(shí)現(xiàn)方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2016-12-12javascript實(shí)現(xiàn)點(diǎn)擊按鈕切換輪播圖功能
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)點(diǎn)擊按鈕切換輪播圖功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09Chrome中模態(tài)對話框showModalDialog返回值問題的解決方法
chrome中彈出模態(tài)對話框,通過window.returnValue賦返回值關(guān)閉后,有的情況下無法取得返回值。2010-05-05微信小程序?qū)W習(xí)總結(jié)(三)條件、模板、文件引用實(shí)例分析
這篇文章主要介紹了微信小程序條件、模板、文件引用,結(jié)合實(shí)例形式分析了微信小程序if條件判斷、模板調(diào)用、wxss文件引用等相關(guān)操作技巧,需要的朋友可以參考下2020-06-06