接收后台返回的文件流并打开
topreFile(url, name) {
wx.showLoading({
title: '加载中'
})
wx.request({
url: downloadFile,
data: { fileUrl: url, fileName: name },
method: 'GET',
header: {
'Content-Type': 'application/json',
'X-Access-Token': wx.getStorageSync('token'),
req_type: 'xcx'
},
responseType: 'arraybuffer',
success: res => {
if (res.statusCode === 200) {
const fs = wx.getFileSystemManager()
fs.writeFile({
filePath: wx.env.USER_DATA_PATH + '/' + `${name}`,
data: res.data,
encoding: 'binary',
success(res) {
wx.openDocument({
filePath: wx.env.USER_DATA_PATH + '/' + `${name}`,
showMenu: true,
success: function(res) {
setTimeout(() => {
wx.hideLoading()
}, 500)
}
})
}
})
}
}
})
}
鸣谢
思路来源