前端调取后端接口后直接下载后端返回的文件流

1.调取接口后端返回的文件流

2. axios请求下载接口主要代码  responseType:'blob'

3.在response设置下载接口返回response

4.文中使用

downLoadBrand(this.chooseItemId).then((res) => {
        console.log("downLoadBrand", res);
        const url = window.URL.createObjectURL(new Blob([res.data]));
        const link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        //获取文件名称
        let downName =res.headers?.get("content- 
                   disposition")?.split("attachment;filename=")[1] || "";
        link.download = downName;
        document.body.appendChild(link);
        link.click();
        URL.revokeObjectURL(link.href);
        document.body.removeChild(link);
      });

主要代码:

let downName =res.headers?.get("content-disposition")?.split("attachment;filename=")[1] || "";//获取文件名称

 

Success !