fix:文件下载功能报500修复

This commit is contained in:
weifashi 2023-11-01 10:06:56 +08:00
parent 0b888b9597
commit 3f11451f3c

View File

@ -2879,9 +2879,16 @@ class Base
public static function streamDownload($file, $name = null) public static function streamDownload($file, $name = null)
{ {
$contentType = 'application/octet-stream'; $contentType = 'application/octet-stream';
if ($name && !str_contains($name, '.')) {
$name .= ".";
}
//
if ($file instanceof \Closure) { if ($file instanceof \Closure) {
$callback = $file; return Response::streamDownload($file, $name, [
} else { 'Content-Type' => $contentType,
]);
}
//
if (!$file instanceof File) { if (!$file instanceof File) {
if ($file instanceof \SplFileInfo) { if ($file instanceof \SplFileInfo) {
$file = new File($file->getPathname()); $file = new File($file->getPathname());
@ -2893,12 +2900,10 @@ class Base
throw new FileException('File must be readable.'); throw new FileException('File must be readable.');
} }
$contentType = $file->getMimeType() ?: $contentType; $contentType = $file->getMimeType() ?: $contentType;
$callback = $file->getContent(); $content = $file->getContent();
} return Response::streamDownload(function() use ($content) {
if ($name && !str_contains($name, '.')) { echo $content;
$name .= "."; }, $name, [
}
return Response::streamDownload($callback, $name, [
'Content-Type' => $contentType, 'Content-Type' => $contentType,
]); ]);
} }