1、流數(shù)據
uni.request({
url: this.$config.apiUrl+“/api/report/content/fill?code=bv.mf.refund.pay.voucher&busiNo=00201323051500148949”,
header: {
‘content-type’: ‘application/json;charset=utf-8’,
‘X-App-Code’: ‘weixin’,
‘X-Source’: ‘program’,
‘X-Token’: store.getters.token
},
responseType:‘arraybuffer’,
success: (data) => {
const fs = uni.getFileSystemManager()
const filePath = wx.env.USER_DATA_PATH + ‘/’ + Date.now() + ‘.pdf’ // wx.env.USER_DATA_PATH 為微信提供的保存文件路徑
console.log(filePath)
fs.writeFile({
filePath,
data: data.data, // 將 base64 轉為 arrayuffer wx.base64ToArrayBuffer(res.replace(/[\r\n]/g, ‘’))
success (res) {
uni.openDocument({
showMenu: true,
fileType: ‘pdf’,
filePath,
success: function (res) {
console.log(‘打開文檔成功’)
}
})
},
fail (err) {
console.log(‘錯誤’, err)
}
})
}
});
2 文件地址模式文章來源地址http://www.zghlxwxcb.cn/news/detail-678745.html
uni.showLoading({
title: '下載中'
});
//下載文件
uni.downloadFile({ //只能是GET請求
url: this.$config.apiUrl+"/api/report/content/fill?code=bv.mf.refund.pay.voucher&busiNo=00201323051500148949", //請求地址(后臺返回的碼流地址)
// url: this.$config.staticUrl+"/emptyOwner.png",
success: (res) => {
//下載成功
if (res.statusCode === 200) {
//保存文件
let tempFile = res.tempFilePath;
//保存成功之后 打開文件
uni.getFileSystemManager().saveFile({
tempFilePath: tempFile,
// filePath: wx.env.USER_DATA_PATH + '/' + '上傳成員.pdf',//自定義文件名
success(res) {
console.log(res)
uni.openDocument({
filePath: res.savedFilePath,
showMenu: true, //是否可以分享
success: (res) => {
uni.hideLoading()
console.log(res);
},
fail: (e) => {
uni.showToast({
title: '打開失敗',
icon: "error"
})
}
})
}
})
}
},
fail: (e) => {
console.log(e, '文件下載失敗')
uni.showToast({
title: '文件下載失敗',
icon: "error",
})
}
});
文章來源:http://www.zghlxwxcb.cn/news/detail-678745.html
到了這里,關于uniapp獲取 pdf文件流 并展示的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網!