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();

});

Riadh Rahmi

Senior Web Developer (Drupal & Laravel)

I am a senior web developer, I have experience in planning and developing large scale dynamic web applications especially in Drupal and Laravel.

Web Posts

Search

Page Facebook