Home > Back-end >  Laravel storage cant force download files
Laravel storage cant force download files

Time:11-29

I want to let user to download some file from storage path. In this case will be some backups they have made. So i have a method can create backups and show on vue js table. And now i am trying to downlaod manyally from vue

<tr v-show="backups.length" v-for="backup in backups" :key="backup.id">
                        <td>{{backup.path}}</td>
                        <td>{{backup.size}} KB</td>
                        <td>
                            <v-button type="sm-secondary" title="Download" @click="downloadBackup(backup.path)">
                                <i ></i>
                            </v-button>
                        </td>
                    </tr>

methods: {
    downloadBackup(path) {
            axios.get('/download-backup-system/' path)
            .then(response => {
                console.log(response)
            })
            .catch(error => {
                console.log(error)
            })
        }
}

//dd($Filepath);
//RETURN
//"C:\...\storage\app/Backup/2022-11-28-10-21-27.zip"

Laravel controller

public function downloadBackups($path)
    {
        $Filepath = storage_path('app/Backup/'.$path);
        if (file_exists($Filepath)) {
            return Response::download($Filepath);
        }
    }

But this does not work and on response i get something like this

PK�.�&_���vt�8̩��....

Also i have tryed this https://stackoverflow.com/questions/48534837/how-to-download-zip-file-through-browser-laravel method, but does not work for me

CodePudding user response:

You're trying to download a file through AJAX. Its probably easier to make a link a with the download path as href:

<tr v-show="backups.length" v-for="backup in backups" :key="backup.id">
    <td>{{backup.path}}</td>
    <td>{{backup.size}} KB</td>
    <td>
        <a :href="`/download-backup-system/`   backup.path">
            Download <i ></i>
        </a>
    </td>
</tr>

CodePudding user response:

i also had a problem at first with downloading files from the storage. i solved it this way

i made a Crud, And foreach row i added a Download button. When clicked this button went to a custom function in my controller with the (fileId).

public function download(Request $request, int $fileId)
// $fileId is the id from the download button
{
        $fullfile = File::find($fileId);
        $downloadfile = File::find($fullfile, ['file'])->pluck('file')->last();

  return response()->download($downloadfile);

}
  • Related