当下载文件接口在请求成功时返回流,失败时返回json格式的报错数据需要提示出来时怎么处理?
看代码吧
- if(res.data.type === "application/json") {
- const reader = new FileReader();
- reader.onload = function(){
- const { errorMsg } = JSON.parse(reader.result);
- //处理错误
- ElMessage.error(errorMsg);
- };
- reader.readAsText(res.data);
- } else {
- const contentDisposition = res.headers['content-disposition'];
- // 此方法为公共方法处理返回流的情况,详细见本博主的上几条博客,这里是传送门
- // https://blog.csdn.net/weixin_41719836/article/details/119326581
- exportExcel(res.data, decodeURIComponent(contentDisposition.split('filename=')[1]));
- }