下载文件
直接请求文件接口文件打开将会失败,例如xlsx打开将会无法解析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| reportApi.getIncome().then(res => { const fileManagerObj = uni.getFileSystemManager() console.log(fileManagerObj); const filePath = `${wx.env.USER_DATA_PATH}/${new Date().getTime()}.xlsx` fileManagerObj.writeFile({ data: res, filePath: filePath, encoding: 'binary', success: (res) => { console.log(res) viewDoc(filePath) } }) })
|
使用uniapp提供的downloadFile函数
1 2 3 4 5 6 7 8 9 10 11 12
| uni.downloadFile({ url: pjConfig.baseUrl + "/rhhs/reports", header: { Authorization: 'Bearer ' + getToken() }, success: res => { if (res.statusCode == 200) { console.log(res) viewDoc(res.tempFilePath) } } })
|
打开文件
1 2 3 4 5 6 7 8 9 10 11
| function viewDoc(filePath) { uni.openDocument({ filePath: filePath, fileType: 'xlsx', showMenu: true, success: (res) => { console.log('打开文档成功') } }) }
|