如何通过浏览器下载zip文件Laravel?
How to download zip file through browser Laravel?
我正在尝试以 user-selected.up 的身份压缩一些图像,现在我想出了一个解决方案 zipping.My 问题是在我将相关数据压缩到 zip 文件后我该如何下载它通过 browser.i 尝试了不同的方法仍然没有 working.when 我创建了 zip 它存储在 public folder.how 我可以通过浏览器从那里下载 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', array('Content-Type: application/zip','Content-Length: '. filesize($fileurl)));
或
return Response::download($fileurl, 'Photos.zip', array('Content-Type: application/octet-stream','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
希望对您有所帮助!
尝试这种方式可能会有帮助。只需 return 您在 ajax 成功压缩文件和路径名
在控制器中
$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,
}
我正在尝试以 user-selected.up 的身份压缩一些图像,现在我想出了一个解决方案 zipping.My 问题是在我将相关数据压缩到 zip 文件后我该如何下载它通过 browser.i 尝试了不同的方法仍然没有 working.when 我创建了 zip 它存储在 public folder.how 我可以通过浏览器从那里下载 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', array('Content-Type: application/zip','Content-Length: '. filesize($fileurl)));
或
return Response::download($fileurl, 'Photos.zip', array('Content-Type: application/octet-stream','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
希望对您有所帮助!
尝试这种方式可能会有帮助。只需 return 您在 ajax 成功压缩文件和路径名 在控制器中
$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,
}