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

Php 如何通过浏览器Laravel下载zip文件?,php,laravel,laravel-4,zip,Php,Laravel,Laravel 4,Zip,我正在尝试将一些图像压缩为用户选择的图像。到目前为止,我已经想出了一个压缩解决方案。我的问题是,在我将相关数据压缩到zip文件后,如何通过浏览器下载它。我尝试了不同的方法仍然不起作用。当我创建zip文件时,它存储在公用文件夹中。我如何从那里下载zip文件通过浏览器 这是我的密码 $photos = json_decode(Input::get('photos')); $dir = time(); foreach ($photos as $file) { /* Log::error(Imag

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

这是我的密码

$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!');
}
作为回应,我得到了如下结果:

尝试更改

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

试着改变

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


问题是您试图通过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'];
}
将您的javascript更改为:

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

希望这对你有帮助

问题是您试图通过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'];
}
将您的javascript更改为:

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

希望这对你有帮助

试试这种方法可能会有帮助。只需在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!');
    }
然后加上这一行

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

试试这个方法也许会有帮助。只需在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!');
    }
然后加上这一行

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


尝试此返回响应()->下载(您的文件名或文件位置)@bipin其不工作尝试:
returnresponse()->download($fileurl,$zipFileName,$headers)->deleteFileAfterSend(true)
@HirenGohel不工作您是否试图通过ajax加载文件?请尝试此返回响应()->下载(您的文件名或文件位置)@bipin其不工作尝试:
returnresponse()->download($fileurl,$zipFileName,$headers)->deleteFileAfterSend(true)@HirenGohel不工作您试图通过ajax加载文件吗?您一定遇到了一些错误。请用错误更新问题。控制台中没有错误。但响应中有一些错误。我已更新了question@joseph即.zip文件的原始内容。您需要重新加载具有完整文件路径的视图才能下载文件。我不明白您是否可以进一步检查我。请确认您正在尝试使用ajax下载文件。?您一定遇到了一些错误。请用错误更新问题。控制台中没有错误。但响应中有一些错误。我已更新了question@joseph即.zip文件的原始内容。你需要用完整的文件路径重新加载视图才能下载文件。我没有弄明白你可以进一步检查我吗请确认你正在尝试使用ajax下载文件。?那么我是否已经用我的ajax请求替换了这两行我用javascript更新了代码你能告诉我在哪里更改吗?我不认为你的ajax代码用于下载您在问题中发布的文件!!我有什么办法可以做到吗this@joseph请查看此演示示例:并在结束此问题时接受并更新我的答案!因此,我必须用我的Ajax请求替换这两行吗?我已经用我的javascript更新了代码。你能告诉我在哪里进行更改吗?我不认为你的Ajax代码用于下载你在问题中发布的文件!!我有什么办法可以做到吗this@joseph请查看此演示示例:并在结束此问题时接受并更新我的答案!