image.png
image.png
方案一:采用微信提供的API:wx.downloadFile + wx.openDocument
方案二:出于安全問題的考慮超营,后臺直接返回url不安全晌纫,所以更換成返回流形式的數(shù)據(jù)來解決:wx.getFileSystemManager + wx.openDocument
方案一:
toPdf() {
uni.showLoading({
title: '加載中...',
})
uni.downloadFile({
url: '后臺直接返回的url路徑',
success: function(res) {
var filePath = res.tempFilePath;
setTimeout(() => {
uni.openDocument({
filePath: filePath,
showMenu: true,
success: function(res) {
uni.$showMsg('打開文檔成功')
},
fail: function(res) {
console.log('open', res);
uni.$showMsg('找不到文件')
}
}, 3000);
})
},
fail: function(res) {
console.log('down', res);
uni.$showMsg('文檔下載失敗')
}
});
},
}
}
方案二:
//后臺返回流文件
toviewBtn(index, id) {
this.id = id
uni.showLoading({
title: '加載中...',
})
uni.request({
url: uni.$http.baseUrl + '調(diào)用的后臺接口',
method: "GET",
success: rest => {
// console.log(rest, '-----');
if (rest.statusCode == 200) {
if (rest.data.Status == 200) {
const fs = wx.getFileSystemManager(); //獲取全局唯一的文件管理器
fs.writeFile({ // 寫文件
filePath: wx.env.USER_DATA_PATH +
"/filename.pdf", // wx.env.USER_DATA_PATH 指定臨時(shí)文件存入的路徑,后面字符串自定義
data: rest.data.PdfInfo,
encoding: "base64", //二進(jìn)制流文件必須是 binary
success(res) {
wx.openDocument({
filePath: wx.env.USER_DATA_PATH + "/filename.pdf", //拿上面存入的文件路徑
showMenu: true,
success: function(res) {
setTimeout(() => {
wx.hideLoading()
}, 500)
}
})
},
});
} else {
uni.$showMsg(rest.data.Message)
}
}
}
})
}
版權(quán)聲明:本文為原創(chuàng)文章滤港,轉(zhuǎn)載請附上原文出處鏈接和本聲明
本文鏈接:http://www.reibang.com/writer#/notebooks/48898357/notes/88678406
方案二參考鏈接地址: https://blog.csdn.net/mythInternet/article/details/109100864