需求:通过接口返回的二进制流数据,这个流数据他是一个xlsx文档,需要给到用户一个文档线上连接。
下面是具体代码,注意只针对二进制的文件数据,如果图片上传直接调用uploadFile就可以,并且兼容原生微信小程序。
export function exportExcel1(query) {
uni.showLoading({
title: '正在导出...',
mask: true
});
// 这个是get接口参数 setUrlQuery 是我自定义的函数封装 主要是拼接url参数的
let obj = {
"personnelIdList":state.personnel.list.map((item)=>item.personnelId),
"projectId":store.project.projectId,
...query
};
// 调用接口 获取二进制流 进行下载并且获取临时文件地址
uni.downloadFile({
url:env.ApiUrl+'/personnel/laborer/exportExcel'+uni.setUrlQuery(obj),
header: {
'content-type': 'application/json',
'Authorization' : 'Bearer ' + storage.getItem('Authorization')
},
success(res){
let tempFilePath = res.tempFilePath; // 获取微信返回的临时系统文件地址
uni.uploadFile({
url: env.ApiUrl + 'xxx', // 上传文件接口
formData: {}, // 除文件外其他所有数据,传对象,会默认转换为 FormData
filePath: tempFilePath, // 上传临时的系统文件地址
header: {
'content-type': 'application/json',
'Authorization' : 'Bearer ' + storage.getItem('Authorization')
},
name: 'file', // 注意与后端约定的字段名称
success(res){
uni.hideLoading();
// 弹窗提示导出成功 用户点击确定复制url文件地址
uni.showModal({
title: '导出成功',
content: '已生成当前列表人员数据文档!',
cancelText: '取消',
confirmText: '复制URL',
success: function (res) {
if (res.confirm) {
// 获取url 进行复制
uni.setClipboardData({
data:result,
success() {},
});
}
}
});
// 文件预览
// uni.openDocument({
// filePath: 'url',// 可以是微信给的系统临时url地址,也可以是正式的线上地址
// success: (sus) => {
// console.log('成功打开');
// },
// });
}
});
}
});
};
浙公网安备 33010602011771号