vue3请求设置responseType: ‘blob‘,导致失败后获取不到返回信息
vue3请求设置responseType: ‘blob’,导致失败后获取不到返回信息
使用FileReader解决
dataCollect().downloadAll(data).then((res: any) => {if (res.type === 'application/json') {const fileReader = new FileReader();fileReader.readAsText(new Blob([res], { type: 'application/octet-stream' }), 'utf-8');fileReader.onload = () => {const result = JSON.parse(fileReader.result);ElMessage.error(result.msg);};return;}// console.log('下载全部', res);let blob = new Blob([res], {type: 'application/zip', //文件类型});let eLink = document.createElement('a');eLink.download = `${fileName}.zip`;eLink.style.display = 'none';eLink.href = URL.createObjectURL(blob);document.body.appendChild(eLink);eLink.click();document.body.removeChild(eLink);URL.revokeObjectURL(eLink.href);}).finally(() => {loading.value = false;});