uni-app 預覽pdf文件


安卓uni-app實現pdf文件預覽功能:

1.https://mozilla.github.io/pdf.js/getting_started/#download下載

放在根目錄下,

2.新建一個webView頁面

 1 <template>
 2     <view style="width: 100%;" >
 3         <web-view :src="allUrl"></web-view>
 4     </view>
 5 </template>
 6 
 7 <script>
 8     export default {
 9         data() {
10             return {
11                 allUrl:'',
12                 viewerUrl: '/hybrid/html/web/viewer.html', // 格式化文件流的一個js 文件
13             }
14         },
15         onLoad(options) {
16             // encodeURIComponent 函數可把字符串作為 URI 組件進行編碼。decodeURIComponent解碼
17             let fileUrl = encodeURIComponent(decodeURIComponent(options.fileUrl)); 
18             this.allUrl = this.viewerUrl + '?file=' + fileUrl
19           }
20     }
21 </script>

3.前一個頁面增加跳轉:

1 // 預覽pdf
2 toViewFile(item) {
3     let fileUrl = `${this.baseUrl}/share/downloadFile?filePath=${item.filePath}&fileName=${item.fileName}`;
4     uni.navigateTo({
5         url: '/pages/webView/webView?fileUrl='+ encodeURIComponent(fileUrl)
6     });
7 },

由於url路徑比較長,帶有&符號,傳參,&后面的會被截斷,故需要加上encodeURIComponent。完美解決pdf無法打開問題。

 

相關網站:https://mozilla.github.io/pdf.js/getting_started/#download   

     https://blog.csdn.net/mwh_user/article/details/112368597

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM