webmaster.vinay
2 years ago
475
1
Laravel

Issue with Laravel Response download and Axios post

Posted 2 years ago by webmaster.vinay

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.

Please sign in or create an account to participate in this conversation.