1、流数据
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 文件地址模式
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",})}});