diff --git a/app/Module/Base.php b/app/Module/Base.php index db805a4f0..9c00cdcf6 100755 --- a/app/Module/Base.php +++ b/app/Module/Base.php @@ -2966,17 +2966,10 @@ class Base */ public static function streamDownload($file, $name = null) { - $contentType = 'application/octet-stream'; if ($name && !str_contains($name, '.')) { $name .= "."; } // - if ($file instanceof \Closure) { - return Response::streamDownload($file, $name, [ - 'Content-Type' => $contentType, - ]); - } - // if (!$file instanceof File) { if ($file instanceof \SplFileInfo) { $file = new File($file->getPathname()); @@ -2987,12 +2980,23 @@ class Base if (!$file->isReadable()) { throw new FileException('File must be readable.'); } - $contentType = $file->getMimeType() ?: $contentType; - $content = $file->getContent(); - return Response::streamDownload(function() use ($content) { - echo $content; - }, $name, [ - 'Content-Type' => $contentType, + // 大于100M直接下载 + if ($file->getSize() > 100 * 1024 * 1024) { + return Response::download($file->getPathname(), $name); + } + // + $filePath = $file->getPathname(); + return Response::stream(function () use ($filePath) { + $fileStream = fopen($filePath, 'r'); + while (!feof($fileStream)) { + echo fread($fileStream, 1024); + flush(); + } + fclose($fileStream); + }, 200, [ + 'Content-Type' => 'application/octet-stream', + 'Content-Disposition' => 'attachment; filename="'.$name.'"', + 'Content-Length' => $file->getSize(), ]); }