uniapp實現線上文件預覽


使用uni.downloadFile、uni.previewImage、uni.openDocument實現文件預覽

 

uni.openDocument(OBJECT)官網描述

新開頁面打開文檔,支持格式:doc, xls, ppt, pdf, docx, xlsx, pptx。

平台差異說明

App H5 微信小程序 支付寶小程序 百度小程序 字節跳動小程序、飛書小程序 QQ小程序 快手小程序
x x

OBJECT 參數說明:

參數名 類型 必填 說明 平台差異說明
filePath String 文件路徑,可通過 downFile 獲得  
fileType String 文件類型,指定文件類型打開文件,有效值 doc, xls, ppt, pdf, docx, xlsx, pptx 微信小程序
showMenu Boolean 右上角是否有可以轉發分享的功能 微信小程序
success String 接口調用成功的回調函數  
fail String 接口調用失敗的回調函數 微信小程序
complete String 接口調用結束的回調函數(調用成功、失敗都會執行)  

 

代碼:

uni.downloadFile({
	 url: this.sjuAjax.fileUrl+fileUrl,
	 success: function (res) {
         if(res.tempFilePath.indexOf(".png" ||".jpg") > -1)
         {
            var filePath = []
            filePath[0]=res.tempFilePath;
            // 預覽圖片
            uni.previewImage({
              urls: filePath,
              success: function (res) {   
                console.log('打開成功')
              },
              fail:function(res){
                console.log(res)
              }
            })
         }
         else
         {
          var filePath = res.tempFilePath;
          uni.openDocument({
                       filePath: filePath,
                       success: function (res) {
                            console.log('打開文檔成功');
                       }
                 });
         }
}, fail:function (res){ console.log(res) } });

 


免責聲明!

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



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