使用axios下载文件流

https://www.jianshu.com/p/ea9e96a4d084

https://www.jianshu.com/p/a79c176688de

 

axios({
      method: "post",
      url: "你的url"
      // 这里可以在header中加一些东西,比如token
    })
      .then(res => {
        console.log("response: ", res);
        // new Blob([data])用来创建URL的file对象或者blob对象
        let url = window.URL.createObjectURL(new Blob([res.data])); 
        // 生成一个a标签
        let link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        // 生成时间戳
        let timestamp=new Date().getTime();   
        link.download = timestamp + ".pdf";   
        document.body.appendChild(link);
        link.click();
      })
      .catch(error => {
        console.log("response: ", error);
      });

vue使用axios下载zip,打开提示文件损坏

解决办法:在调用后端接口的时候增加两个参数:

responseType: 'blob',headers:{ 'Content-Type': 'application/json; application/octet-stream'},

posted @ 2021-07-13 09:14  zhao1949  阅读(1372)  评论(0编辑  收藏  举报