mirror of
https://github.com/kuaifan/dootask.git
synced 2026-02-23 17:30:33 +00:00
fix: 手机下载文件名出现html的情况
This commit is contained in:
parent
c587a50505
commit
acddde8faa
@ -1161,9 +1161,7 @@ class DialogController extends AbstractController
|
||||
}
|
||||
//
|
||||
$filePath = public_path($array['path']);
|
||||
return Base::streamDownload(function() use ($filePath) {
|
||||
echo file_get_contents($filePath);
|
||||
}, $array['name']);
|
||||
return Base::streamDownload($filePath, $array['name']);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -1659,9 +1659,7 @@ class ProjectController extends AbstractController
|
||||
}
|
||||
//
|
||||
$filePath = public_path($file->getRawOriginal('path'));
|
||||
return Base::streamDownload(function() use ($filePath) {
|
||||
echo file_get_contents($filePath);
|
||||
}, $file->name);
|
||||
return Base::streamDownload($filePath, $file->name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -112,9 +112,7 @@ class FileContent extends AbstractModel
|
||||
} else {
|
||||
$filePath = public_path($content['url']);
|
||||
}
|
||||
return Base::streamDownload(function() use ($filePath) {
|
||||
echo file_get_contents($filePath);
|
||||
}, $name);
|
||||
return Base::streamDownload($filePath, $name);
|
||||
}
|
||||
if (empty($content)) {
|
||||
$content = match ($file->type) {
|
||||
@ -143,9 +141,7 @@ class FileContent extends AbstractModel
|
||||
if ($download) {
|
||||
$filePath = public_path($path);
|
||||
if (isset($filePath)) {
|
||||
return Base::streamDownload(function() use ($filePath) {
|
||||
echo file_get_contents($filePath);
|
||||
}, $name);
|
||||
return Base::streamDownload($filePath, $name);
|
||||
} else {
|
||||
abort(403, "This file not support download.");
|
||||
}
|
||||
|
||||
@ -11,6 +11,8 @@ use Redirect;
|
||||
use Request;
|
||||
use Response;
|
||||
use Storage;
|
||||
use Symfony\Component\HttpFoundation\File\Exception\FileException;
|
||||
use Symfony\Component\HttpFoundation\File\File;
|
||||
use Validator;
|
||||
|
||||
class Base
|
||||
@ -2870,15 +2872,35 @@ class Base
|
||||
|
||||
/**
|
||||
* 流下载,解决没有后缀无法下载的问题
|
||||
* @param $callback
|
||||
* @param $file
|
||||
* @param $name
|
||||
* @return mixed
|
||||
*/
|
||||
public static function streamDownload($callback, $name = null) {
|
||||
public static function streamDownload($file, $name = null)
|
||||
{
|
||||
$contentType = 'application/octet-stream';
|
||||
if ($file instanceof \Closure) {
|
||||
$callback = $file;
|
||||
} else {
|
||||
if (!$file instanceof File) {
|
||||
if ($file instanceof \SplFileInfo) {
|
||||
$file = new File($file->getPathname());
|
||||
} else {
|
||||
$file = new File((string)$file);
|
||||
}
|
||||
}
|
||||
if (!$file->isReadable()) {
|
||||
throw new FileException('File must be readable.');
|
||||
}
|
||||
$contentType = $file->getMimeType() ?: $contentType;
|
||||
$callback = $file->getContent();
|
||||
}
|
||||
if ($name && !str_contains($name, '.')) {
|
||||
$name .= ".";
|
||||
}
|
||||
return Response::streamDownload($callback, $name);
|
||||
return Response::streamDownload($callback, $name, [
|
||||
'Content-Type' => $contentType,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user