How to Download File using Axios Vue JS?

Posted by LaravelIndia - 3 years ago

You can see bellow peace of code for axios request example:

<!DOCTYPE html>
    <title>How to Download File using Axios Vue JS? -</title>
    <script src=""></script>
    <script src="" integrity="sha256-S1J4GVHHDMiirir9qsXWc8ZWw74PHHafpsHp5PXtjTs=" crossorigin="anonymous"></script>

<div id="app">

  <button @click="onClick()">DownLoad</button>


<script type="text/javascript">

    var app = new Vue({
      el: '#app',
      methods: {
          onClick() {
                    url: 'http://localhost:8000/my.pdf',
                    method: 'GET',
                    responseType: 'blob',
                }).then((response) => {
                     var fileURL = window.URL.createObjectURL(new Blob([]));
                     var fileLink = document.createElement('a');

                     fileLink.href = fileURL;
                     fileLink.setAttribute('download', 'file.pdf');