如何通过浏览器 Laravel 下载 zip 文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48534837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:19:20  来源:igfitidea点击:

How to download zip file through browser Laravel?

phplaravellaravel-4zip

提问by joseph

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?

我正在尝试将一些图像压缩为用户选择的。到目前为止,我已经提出了一个压缩解决方案。我的问题是将相关数据压缩为 zip 文件后如何通过浏览器下载它。我尝试了不同的方法仍然不起作用。当我创建 zip 时,它存储在公共文件夹中。如何通过浏览器从那里下载 zip 文件?

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

作为回应,我得到了这样的信息: 在此处输入图片说明

回答by Hiren Gohel

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.

问题是您正在尝试通过 AJAX 加载文件,而您无法按照您尝试的方式进行加载。

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 javascriptto:

将您的javascript更改为:

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

Hope this helps you!

希望这对你有帮助!

回答by bipin

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

尝试这种方式可能会有所帮助。只需在控制器中以ajax成功返回您的zip文件和路径名

    $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,
          }

回答by Sushant Pimple

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)));