Last Updated on February 23, 2022 by Justin Su
This article is a tutorial and simple guide on how to download files with Axios. Let’s start learning how it works.
What Is Axios?
Axios is a simple promise-based HTTP client for the browser and node.js. Axios provides a simple to use the library in a small package with a very extensible interface.
It has been used mainly for basic HTTP GET & POST requests.
Also Read: How To Start Quiz Site
Also Read: How To Create Micro Job Sites
How To Download Files With Axios
Use the following code provided by Javilobo user on GitHub. You can use to download pdf and excel using the code easily.
axios({
url: ‘http://api.dev/file-download’, //your url
method: ‘GET’,
responseType: ‘blob’, // important
}).then((response) => {
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement(‘a’);
link.href = url;
link.setAttribute(‘download’, ‘file.pdf’); //or any other extension
document.body.appendChild(link);
link.click();
});
Click here to visit the official website of Axios.