Download Files with Axios

Last Updated at : March 6, 2018 By - Usama Muneer | 20402

    You must be familiar with axios calls for API consumption, but what about getting the files in response and render those files to the user for download. We got your covered, the below snippet is tested and works well.

      url: '',
      method: 'GET',
      responseType: 'blob', // important
    }).then((response) => {
       const url = window.URL.createObjectURL(new Blob([]));
       const link = document.createElement('a');
       link.href = url;
       link.setAttribute('download', 'file.pdf'); //or any other extension

    Credits to this Javilobo for his useful Gist.

    blog user
    Usama Muneer

    A web enthusiastic, self-motivated & detail-oriented professional Full-Stack Web Developer from Karachi, Pakistan with experience in developing applications using JavaScript, WordPress & Laravel specifically. Loves to write on different web technologies with an equally useful skill to make some sense out of it.

    Related Posts

    Lots of developers have been a fan of the short arrow function usage since their addition to programming languages. In JavaScript, it has helped make coding functions/closures more concise.

    To expand the business across the globe your application needs some specific changes. There are many ways to handle localization in an application but today we will have to look at how to handle it on frontend using ReactJs,i18next.

    Well, that was it, now we can simply jump to validating our form data with jQuery validate plugin & see how its done in no time.

    Vue.js has taken the front-end development world by storm over the last three years. The project, however, has been around much longer than that. It started in 2013 as a one-man project by Evan You.