长春网站设计制作培训,周口网站推广,wordpress免费企业主题下载,广州公司注册费用问题
使用bold类型从后端接口获取文件流#xff0c;获取成功的时候通过a标签下载#xff1b;失败的时候#xff0c;后端返回的是json#xff0c;这个时候就无法向用户展示后端返回的错误提示信息。
思路
根据返回类型是否为 application/json 区分是否返回成功#xff…问题
使用bold类型从后端接口获取文件流获取成功的时候通过a标签下载失败的时候后端返回的是json这个时候就无法向用户展示后端返回的错误提示信息。
思路
根据返回类型是否为 application/json 区分是否返回成功若失败将blob格式转换为json格式。
代码 this.$http.post(/file/export, { exportType: xlsx }, { responseType: blob }).then(res {if (res.data.type application/json) {const reader new FileReader()reader.readAsText(res.data, utf-8)reader.onload () {const result JSON.parse(reader.result)this.$message.error(result.message)}} else if (res.data.type application/octet-stream) {const downloadUrl window.URL.createObjectURL(new Blob([res.data]))const link document.createElement(a)link.href downloadUrldocument.body.appendChild(link)link.click()} else {this.$message.error(导出文件类型错误)}}).catch(error {this.$message.error(导出文件失败)})