1.示例圖?
2.代碼
fileId:要預覽的pdf文件的id?
viewsFiles(fileId) {wx.showLoading({title: '加載中...'});var params = {url: "/common/getFile/" + fileId ,//后端提供的接口method: "GET",responseType: "arraybuffer",callBack: (res) => {console.log(res)const fs = wx.getFileSystemManager(); //獲取全局唯一的文件管理器fs.writeFile({// 寫文件filePath: wx.env.USER_DATA_PATH + "/" + '文件名.pdf', // wx.env.USER_DATA_PATH 指定臨時文件存入的路徑,后面字符串自定義data: res,encoding: "binary", //二進制流文件必須是 binarysuccess(res) {wx.openDocument({// 新開頁面打開文檔filePath: wx.env.USER_DATA_PATH + "/" + '文件名.pdf', //拿上面存入的文件路徑showMenu: true, // 是否顯示右上角菜單(3個點)success: function (res) {setTimeout(() => {wx.hideLoading();}, 500);},});},});}}http.request(params)},
console.log(res)?打印的內容:
后端接口返回的內容樣式: