diff --git a/app/controller/admin/system/FileController.php b/app/controller/admin/system/FileController.php index 1dda544..c02e145 100644 --- a/app/controller/admin/system/FileController.php +++ b/app/controller/admin/system/FileController.php @@ -13,6 +13,27 @@ use think\response\Json; class FileController extends BaseController { + public function download() + { + $fileId = \request()->route('fileId'); + $fileExt = hashids(12)->decode($fileId)[0] ?? ''; + $data = \app\entity\SysFileRecord::find($fileExt); + + $fileUrl = filesystem()->getFileUrl($data['rule_id'], $data['id'], $data['path'].'/'.$data['name'], true); + if(empty($fileUrl)){ + return json(['message'=>'文件不存在']); + } + $mime = "image/jpeg"; +// header('Content-Type: ' . $mime); +// header('Content-Disposition: attachment; filename="b7b9f5caae81dc32ba2a062e00247270.jpg"'); +// header('Content-Length: 928318'); +// echo file_get_contents($fileUrl); + return \response()->content(file_get_contents($fileUrl))->header([ + 'Content-Type'=> $mime, + 'Content-Disposition'=> 'attachment; filename="b7b9f5caae81dc32ba2a062e00247270.jpg"', + 'Content-Length' => 928318 + ]); + } /** * 分页查询账号登录记录 * @return Json diff --git a/route/api.php b/route/api.php index a69d077..92173c0 100644 --- a/route/api.php +++ b/route/api.php @@ -23,26 +23,8 @@ use app\http\middleware\AuthMiddleware; use think\facade\Route; use think\middleware\AllowCrossDomain; -Route::get("/i/down/:fileId", function (){ -// return json(['message'=>'暂不支持下载']); - $fileId = \request()->route('fileId'); - $fileExt = hashids(12)->decode($fileId)[0] ?? ''; - $data = \app\entity\SysFileRecord::find($fileExt); - - $fileUrl = filesystem()->getFileUrl($data['rule_id'], $data['id'], $data['path'].'/'.$data['name'], true); - if(empty($fileUrl)){ - return json(['message'=>'文件不存在']); - } - $mime = "image/jpeg"; - header('Content-Type: ' . $mime); - header('Content-Disposition: attachment; filename="b7b9f5caae81dc32ba2a062e00247270.jpg"'); - header('Content-Length: 928318'); - - echo file_get_contents($fileUrl); - exit; - -}); - +// 文件下载 +Route::get("/i/down/:fileId", [FileController::class, 'download']); Route::get("/i/:fileId", function (){ $fileId = \request()->route('fileId'); $fileExt = hashids(12)->decode($fileId)[0] ?? ''; @@ -54,14 +36,8 @@ Route::get("/i/:fileId", function (){ } $mime = "image/jpeg"; header('Content-Type: ' . $mime); -// header('Content-Disposition: attachment; filename="b7b9f5caae81dc32ba2a062e00247270.jpg"'); -// header('Content-Length: 928318'); echo file_get_contents($fileUrl); exit; -// $fileCo = file_get_contents($fileUrl); -// $base64Image = base64_encode($fileCo); -// echo 'My Image'; -// exit; }); Route::group("adminapi", function () {