JyoKou  

这两天写一个后台生成Excel返回前端下载的功能,遇到了一个问题,记录一下。

前端点击下载按钮,文档损坏,但是使用Postman调用下载,文档却是正常的。

exportExcel(exportExcelParams.value).then((res: any) => {
        let blob = new Blob([res], {
          type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8',
        });
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = URL.createObjectURL(blob);
        link.download = exportExcelParams.value.expName; //下载的文件名
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });

在请求方法中加入  responseType: 'blob' 后,问题解决,如下所示:

export const exportExcel = async (SysExportSetReq: any) => {
  return request.request({
    url: '/sam/master/baSysExportSet/exportExcel',
    method: 'POST',
    data: SysExportSetReq,
    responseType: 'blob'
  });
};

 

posted on 2022-08-03 09:46  JyoKou  阅读(5073)  评论(0编辑  收藏  举报