VinayKesharwani's avatar

Issue with Laravel Response download and Axios post

I am using Laravel 5.5 with Vue.js 2.4. This SPA uses Axios library for request. I was trying to post Axios request and on successful operation, one file was supposed to be downloaded.

Here is my axios post request

axios.post('/api/download/file',{id:id})
    .then(response => {
         console.log(response.data);
    }).catch(error => {
         console.log(error.response.data);
    });

My controller method is as following:

public function download(){
    $download_path = storage_path('app/'.$id.'.zip');

    $headers = ['Content-Type: application/zip','Content-Disposition: attachment'];

    return response()->download($download_path, $id.'.zip',$headers);
}

On request, file dialogue is not opening nor the file is downloaded. However the contents are sent back to the success response.

How file can be downloaded using Axios post & Laravel response()->download() method? Any help will be appreciated. Thanks in advance.

0 likes
1 reply
davidker's avatar

i have the same problem , try something like this

axios.post('/api/download/file',{id:id})
   .then(response => {

       
   window.open('/api/download/file/'+id});

   }).catch(error => {

        console.log(error.response.data);
});
public function download(){
   $download_path = storage_path('app/'.$id.'.zip');
$filename=$id.'zip';
   $headers = ['Content-Type: application/zip','Content-Disposition: attachment; filename={$filename}'];

   return response($download_path, 200,$headers);
}

Please or to participate in this conversation.