download(){
wx.showLoading({
title: '加载中',
})
wx.request({
url: '', //调用后台接口的全路径
data: {memberId: this.data.member.id},
method: "GET",
header: {
'Content-type': 'application/x-www-form-urlencoded',
'Cookie': app.globalData.userInfo && app.globalData.userInfo.cookie ? app.globalData.userInfo.cookie : '',
},
responseType: 'arraybuffer', //此处是请求文件流,必须带入的属性
success: res => {
if (res.statusCode === 200) {
const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
fs.writeFile({
filePath: wx.env.USER_DATA_PATH + "/身体成分报告.pdf", // wx.env.USER_DATA_PATH 指定临时文件存入的路径,后面字符串自定义
data: res.data,
encoding: "binary", //二进制流文件必须是 binary
success (res){
wx.openDocument({ // 打开文档
filePath: wx.env.USER_DATA_PATH + "/身体成分报告.pdf", //拿上面存入的文件路径
showMenu: true, // 显示右上角菜单
success: function (res) {
setTimeout(()=>{wx.hideLoading()},500)
}
})
}
})
}
}
})
}
微信小程序接收后台返回的文件流并打开
最新推荐文章于 2024-06-06 22:20:38 发布