Contents

Download file streams with Axios

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
const axios = require('axios').default;

download() {
let downloadUri = '/api/download';
let params = {};
axios.get(downloadUri, {
params:params,
responseType: 'blob',
}).then((response) => {
const headerval = response.headers['content-disposition'];
let filename = headerval.split(';')[1].split('=')[1].replace('"', '').replace('"', '');
filename = decodeURI(filename);
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', filename);
document.body.appendChild(link);
link.click();
});
}

Notice:

  • responseType: 'blob'
  • filename = decodeURI(filename)
Contents