我这里下载的文件流是word格式
handleDownload() {
let that = this;
wx.request({
url: url,
header: { // 指定合理的 Content-Type 字段,以保证客户端正确处理文件类型
'content-type': 'application/json',
'Authorization': wx.getStorageSync('token') // 我这里需要带token验证
},
method: 'post',
responseType: "arraybuffer",
data: {},
success: (res) => {
const path = res.tempFilePath; // 获取下载后存储路径(本地路径)
// openDocument 新开页面打开文档。注意:微信客户端7.0.12版本前默认展示右上角菜单按钮,之后需要传入showMenu;
const fs = wx.getFileSystemManager(); //获取全局唯一的文件管理器
fs.writeFile({ // 写文件
filePath: wx.env.USER_DATA_PATH + "/授权文件.doc",
// 接口返回的是word文件,这里必须写.doc后缀!!打印的res的文件后缀是docx,但是后缀写docx打不开文件,写doc就可以
data: res.data,
encoding: "binary", //二进制流文件必须是 binary
success (res2){
console.log(res,res2,'----');
wx.openDocument({ // 新开页面打开文档
filePath: wx.env.USER_DATA_PATH + "/授权文件.doc", //同上
showMenu: true,
success: function (x) {
console.log(x,'9999');
},
fail: (err) => {
// 文件下载失败的相关处理
that.setData({
moban:true,
})
setTimeout(function () {
that.setData({
moban:false,
})
}, 2000)
}
})
}
});
},
fail(err) {
that.setData({
moban:true,
})
setTimeout(function () {
that.setData({
moban:false,
})
}, 2000)
}
})
},