【vue】后端返回一个文件流(blob对象)时,如何将其下载下来

关键要点:responseType: “blob”,

this.$axios({
    
    
        url: "/api/device/deviceInfo/createDeviceInfoByExcel",
        method: "post",
        headers: {
    
    
          "Content-Type": "multipart/form-data",
          token: localStorage.getItem("token"),
        },
        data: formData,
        responseType: "blob",//关键点
      }).then((res) => {
    
    
          console.log(res);
          //下载文件操作
          const link = document.createElement("a");
          let blob = new Blob([res.data], {
    
     type: "application/vnd.ms-excel" });//生成blob对象
          console.log(blob)
          link.style.display = "none";
          link.href = URL.createObjectURL(blob);
          link.setAttribute("download", "冲突设备" + ".xls"); //下载的文件名以及文件格式
          document.body.appendChild(link);
          link.click();
          document.body.removeChild(link);
      });

猜你喜欢

转载自blog.csdn.net/GongWei_/article/details/113367974