download file with laravel

62 views Asked by At

I´m trying to download a zip file with laravel. But my problem it´s this zip, it´s not downloaded, but in webrowser console in tab network, i can see my file encoded:

enter image description here

My file it´s in my local webserver:

enter image description here

But webrowser, not download it i received a 302 code in the first petition in console, and after 200 code.

to build my logic to download file, i´m doing this:

if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
                    $filesToZip = [
                        $path . '/' . $fileName,
                    ];
                    foreach ($filesToZip as $file) {
                        $zip->addFile($file, basename($file));
                    }

                    if(!file_exists($zipPath)) {
                        return redirect()->back()->withErrors(['error', 'No se ha podido encontrar el fichero generado']);
                    }else{
                        $path = Storage::path('public/contracts/contratos_seleccionados.zip');

                        if (file_exists($path)) {
                            return redirect()->route('admin.downloadZipBills');
                        }
                    }

I´m building my zip file, and if my file exists, i´m calling other route that call function to download my file. My route it´s a type GET

In my downloadZipBills route i have a function, that contains this code:

try{
            $path = Storage::path('public/contracts/contratos_seleccionados.zip');
            return response()->download($path);

        } catch(Exception $e){

This path it´s correct, and if a do dd() and copy/paste this path in other tab, my file its downloaded. But this URL, its a local path from windows. I trayed return this path, and with javascript to do a window.location or window.href, but chrome block this path.

I'm doing it like this because i tryed with other method, and never dowload my file and now i try with this method. I don´t know that i´m doing wrong.

I hope that anybody can help me with this problem. Thanks for readme and sorry for my bad english

0

There are 0 answers