Download file using ajax

08.06.20

The task of downloading a file using ajax may be more difficult than it seems at first glance.

First option

Send the file as base64. Substitute the href attribute. And programmatically click on the link.
There may be problems with files larger than 8 mb

Second option

We send the raw code. And use AXIOS.

axios({
    method: 'POST',
    url,
    responseType: 'blob',
    data
}).then( res => {

    let fileData = window.URL.createObjectURL(new Blob([res.data]));

    //set fileData to href...

}).catch( err => {
   console.log(err);
})

Substitute the result in the href attribute. And programmatically click on the link.

 

blog comments powered by Disqus