joseph
joseph

Reputation: 80

How to download zip file through browser Laravel?

I am trying to zip some images as user-selected.up to now I have come up with a solution for zipping.My problem is after I zip the relevant data to zip file how can I download it through the browser.i have tried different methods still not working.when I create zip it stored in the public folder.how can I download the zip file from there through the browser?

Here is my code

$photos = json_decode(Input::get('photos'));
$dir = time();
foreach ($photos as $file) {
   /* Log::error(ImageHandler::getUploadPath(false, $file));*/
   $imgName = last(explode('/', $file));
   $path = public_path('downloads/' . $dir);
    if (!File::exists($path)) {
        File::makeDirectory($path, 0775, true);
    }
    ImageHandler::downloadFile($file, $path . '/' . $imgName);
}

$rootPath = realpath($path);
$zip_file = 'Photos.zip';
$public_dir = public_path();
$zip = new ZipArchive();
$zip->open($zip_file, ZipArchive::CREATE | ZipArchive::OVERWRITE);
/** @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
         new RecursiveDirectoryIterator($rootPath),
         RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file1) {
    // Skip directories (they would be added automatically)
    if (!$file1->isDir()) {
        // Get real and relative path for current file
        $filePath = $file1->getRealPath();
        $relativePath = substr($filePath, strlen($rootPath) + 1);
        // Add current file to archive
        $zip->addFile($filePath, $relativePath);
     }
}

// Zip archive will be created only after closing object
$zip->close();
$fileurl = public_path()."/Photos.zip";
if (file_exists($fileurl))
{
      return Response::download($fileurl, 'Photos.zip', ['Content-Length: '. filesize($fileurl)]);
} else {
      exit('Requested file does not exist on our server!');
}

In response i'm getting something like this: enter image description here

Upvotes: 1

Views: 19959

Answers (3)

bipin
bipin

Reputation: 419

try this way may be it help. just return you zip file and path name in ajax success in controller

    $fileurl = public_path()."/Photos.zip";
    if (file_exists($fileurl))
    {
        return response()->json($fileurl);
    }
    else
    {
        exit('Requested file does not exist on our server!');
    }

then add this line

      success: function (data) {
         'download': data.fileurl,
          }

Upvotes: 0

Hiren Gohel
Hiren Gohel

Reputation: 5042

The problem is you're trying to load the file via AJAX, which you can't do the way that you're trying to do it.

if (file_exists($fileurl)) {
    return Response::download($fileurl, 'Photos.zip', array('Content-Type: application/octet-stream','Content-Length: '. filesize($fileurl)))->deleteFileAfterSend(true);
} else {
    return ['status'=>'zip file does not exist'];
}

Change your javascript to:

let xhr = new XMLHttpRequest(),  self = this;
window.location = window.location.origin+'/download-file/' + this.selected

Hope this helps you!

Upvotes: 7

Sushant Pimple
Sushant Pimple

Reputation: 1535

Try changing

return Response::download($fileurl, 'Photos.zip', ['Content-Length: '. filesize($fileurl)]);

to

return Response::download($fileurl, 'Photos.zip', array('Content-Type: application/zip','Content-Length: '. filesize($fileurl))); 

OR

return Response::download($fileurl, 'Photos.zip', array('Content-Type: application/octet-stream','Content-Length: '. filesize($fileurl)));

Upvotes: 0

Related Questions