Force download GET request using axios

后端 未结 6 1691
生来不讨喜
生来不讨喜 2020-12-07 16:39

I\'m using vuejs 2 + axios. I need to send a get request, pass some params to server, and get a PDF as a response. Server uses Laravel.

So

axios.get(         


        
6条回答
  •  攒了一身酷
    2020-12-07 17:07

    Try this: It works perfectly for me with compatibility for Internet Explorer 11 (createObjectURL doesn't work on Explorer 11)

    axios({
      url: 'http://vvv.dev',
      method: 'GET',
      responseType: 'blob', // important
    }).then((response) => {
      if (!window.navigator.msSaveOrOpenBlob){
        // BLOB NAVIGATOR
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'download.pdf');
        document.body.appendChild(link);
        link.click();
      }else{
        // BLOB FOR EXPLORER 11
        const url = window.navigator.msSaveOrOpenBlob(new Blob([response.data]),"download.pdf");
      }
    });
    

    https://gist.github.com/javilobo8/097c30a233786be52070986d8cdb1743

提交回复
热议问题