后端传回来的是二进制数据
import store from '@/store'
import config from '@/config'
import {getToken} from '@/utils/auth2'
import * as getTokens from '@/utils/auth'
import errorCode from '@/utils/errorCode'
import {toast,showConfirm,tansParams} from '@/utils/common'
//文件下载
export function downloadFn(fileUrl, data, name) { //地址,参数,文件名称
uni.request({
url: config.baseUrl + fileUrl, // 你的文件下载地址
method: 'POST',
header: {
Authorization: 'Bearer ' + getTokens.getToken() //惠享家的tokrn
},
data: data,
responseType: 'arraybuffer', // 设置响应类型为arraybuffer以接收二进制数据
success: (res) => {
const fileManagerObj = uni.getFileSystemManager() // 获取全局的文件管理器
console.log(fileManagerObj);
// 文件存储到本地的路径
const filePath = `${wx.env.USER_DATA_PATH}/${new Date().getTime()}.xlsx`
fileManagerObj.writeFile({
data: res.data, // 拿到的arraybuffer数据
filePath: filePath,
encoding: 'binary',
success: (res) => {
console.log(res) // 成功了的话这里会打印 writeFile:ok
viewDoc(filePath)
}
})
},
fail: (err) => {
console.error('文件下载失败', err);
}
});
}
// 打开文件
function viewDoc(filePath) {
uni.openDocument({
// 直接打开
filePath: filePath, // 这里填上面写入本地的文件路径
fileType: 'xlsx',
showMenu: true, // 右上角是否有可以转发分享的功能,配不配随意
success: (res) => {
console.log('打开文档成功')
}
})
}
使用
signupExport() {
this.downloadFn('/school/signup/export',{},`模拟志愿表${new Date().getTime()}.xlsx`)
},
评论 暂无