batch-zip-download-btn按钮功能实现

This commit is contained in:
Chenx221 2024-02-15 15:08:17 +08:00
parent db903bd4b3
commit e7eed036f1
Signed by: chenx221
GPG Key ID: D7A9EC07024C3021
2 changed files with 109 additions and 5 deletions

View File

@ -35,6 +35,7 @@ class HomeController extends Controller
'upload' => ['POST'], 'upload' => ['POST'],
'new-folder' => ['POST'], 'new-folder' => ['POST'],
'download-folder' => ['GET'], 'download-folder' => ['GET'],
'multi-ff-zip-dl' => ['POST'],
], ],
], ],
] ]
@ -397,4 +398,83 @@ class HomeController extends Controller
return Yii::$app->response->sendFile($zipPath); return Yii::$app->response->sendFile($zipPath);
} }
} }
/**
* @return \yii\console\Response|Response
* @throws NotFoundHttpException
*/
public function actionMultiFfZipDl()
{
// 获取请求中的文件和文件夹的相对路径
$relativePaths = Yii::$app->request->post('relativePaths');
// 对相对路径进行解码
$relativePaths = array_map('rawurldecode', $relativePaths);
// 初始化总大小
$totalSize = 0;
foreach ($relativePaths as $relativePath) {
// 检查相对路径是否只包含允许的字符
if (!preg_match($this->pattern, $relativePath) || $relativePath === '.' || $relativePath === '..' || str_contains($relativePath, '../')) {
throw new NotFoundHttpException('Invalid file path.');
}
// 确定文件或文件夹的绝对路径
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
// 检查文件或文件夹是否存在
if (!file_exists($absolutePath)) {
throw new NotFoundHttpException('File or directory not found.');
}
// 计算文件或文件夹的大小,并添加到总大小
if (is_dir($absolutePath)) {
$totalSize += $this->getDirectorySize($absolutePath);
} else {
$totalSize += filesize($absolutePath);
}
}
// 检查总大小是否超过200MB
if ($totalSize > 200 * 1024 * 1024) {
throw new NotFoundHttpException('Total size exceeds the limit of 200MB.');
}
// 创建一个新的ZipArchive实例
$zip = new ZipArchive();
$tempDir = sys_get_temp_dir() . '/';
$zipPath = $tempDir . time() . '.zip';
if ($zip->open($zipPath, ZipArchive::CREATE | ZipArchive::OVERWRITE)) {
foreach ($relativePaths as $relativePath) {
$absolutePath = Yii::getAlias(Yii::$app->params['dataDirectory']) . '/' . Yii::$app->user->id . '/' . $relativePath;
if (is_file($absolutePath)) {
// 如果是文件,直接添加到 zip 文件
$zip->addFile($absolutePath, $relativePath);
} else if (is_dir($absolutePath)) {
// 如果是目录,获取目录中的所有文件并添加到 zip 文件
$files = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($absolutePath),
\RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
$relativePathInZip = $relativePath . '/' . substr($filePath, strlen($absolutePath) + 1);
$zip->addFile($filePath, $relativePathInZip);
}
}
}
}
$zip->close();
}
// 发送下载
return Yii::$app->response->sendFile($zipPath);
}
} }

View File

@ -22,7 +22,6 @@ $(document).on('click', '.delete-btn', function () {
$('#deleteModal').modal('show'); $('#deleteModal').modal('show');
}); });
$(document).on('click', '.file-upload-btn', function () { $(document).on('click', '.file-upload-btn', function () {
// 触发文件输入元素的点击事件
$('#file-input').click(); $('#file-input').click();
}); });
$('#file-input').on('change', function () { $('#file-input').on('change', function () {
@ -30,7 +29,6 @@ $('#file-input').on('change', function () {
}); });
$(document).on('click', '.folder-upload-btn', function () { $(document).on('click', '.folder-upload-btn', function () {
// 触发文件输入元素的点击事件
$('#folder-input').click(); $('#folder-input').click();
}); });
@ -59,8 +57,34 @@ $(document).on('click', '.single-download-btn', function () {
}); });
$(document).on('click', '.batch-zip-download-btn', function () { $(document).on('click', '.batch-zip-download-btn', function () {
console.log('批量打包并下载按钮被点击'); var relativePaths = $('.select-item:checked').map(function () {
// 在这里添加你的代码 return $(this).data('relativePath');
}).get();
// 创建一个新的表单
var form = $('<form>', {
action: 'index.php?r=home%2Fmulti-ff-zip-dl',
method: 'post'
});
// 将相对路径添加到表单中
$.each(relativePaths, function(index, value) {
form.append($('<input>', {
type: 'hidden',
name: 'relativePaths[]',
value: value
}));
});
// 添加 CSRF 令牌
form.append($('<input>', {
type: 'hidden',
name: '_csrf',
value: $('meta[name="csrf-token"]').attr('content')
}));
// 将表单添加到页面中并提交
form.appendTo('body').submit();
}); });
$(document).on('click', '.batch-zip-btn', function () { $(document).on('click', '.batch-zip-btn', function () {