Please Disable Your Browser Adblock Extension for our site and Refresh This Page!

our ads are user friendly, we do not serve popup ads. We serve responsible ads!

Refresh Page
Skip to main content
On . By CodimTh
Category:

Vue + Laravel: How to properly Export/Download file

 

 

Example How to download generated csv file.

 

Laravel return the file as a response of an API GET request.

return Excel::download(new DataExport($data), $filename);

 

Then inside Vue web app you need to get the file and download it:

downloadFile() {
  AXIOS.get(this.appApiPath + '/testpdf',
       {responseType: 'arraybuffer'})
   .then(response => {
        var fileURL = window.URL.createObjectURL(new Blob([response.data]));
        var fileLink = document.createElement('a');
        fileLink.href = fileURL;
        fileLink.setAttribute('download', 'test.xlsx');
       document.body.appendChild(fileLink);
       fileLink.click();
    })
}

 

 

Example How to download generated pdf.

from laravel backend:

$pdf = PDF::loadView('users.pdf', ['data' => $data]);
return $pdf->output();

 

from vuejs frontend:

axios({
url: 'http://localhost:8000/api/your-route',
method: 'GET',
{responseType: 'blob'},
}).then((response) => {
     var fileURL = window.URL.createObjectURL(new Blob([response.data], {type: 'application/pdf'}));
     var fileLink = document.createElement('a');
     fileLink.href = fileURL;
     fileLink.setAttribute('download', 'file.pdf');
     document.body.appendChild(fileLink);
     fileLink.click();

});

Comments

Add new comment

Restricted HTML

Search

Page Facebook

Become a patron

If you need some help or you search a Drupal freelancer don't hesitate to contact us.

 

Contact Us

All the content is FREE but I still need your help

 

Become a patreon