From cdd7799cac33f54218bf20c8fb90df3be1f798e3 Mon Sep 17 00:00:00 2001 From: topnuomi <1130395124@qq.com> Date: Sat, 30 May 2020 14:19:42 +0800 Subject: [PATCH] =?UTF-8?q?Response=E7=B1=BB=E6=96=B0=E5=A2=9EsendFile?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- framework/library/Application.php | 6 ++-- .../library/exception/ResponseException.php | 19 ++++++++++++ framework/library/http/Request.php | 1 - framework/library/http/Response.php | 31 ++++++++++++------- 4 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 framework/library/exception/ResponseException.php diff --git a/framework/library/Application.php b/framework/library/Application.php index 71131e7..74da3a6 100644 --- a/framework/library/Application.php +++ b/framework/library/Application.php @@ -83,10 +83,10 @@ class Application // 处理请求并得到数据 $response = Response::instance()->header([ - 'X-Powered-By: TOP-Framework' + 'X-Powered-By: TOP-Framework', ])->send($router->execute()); - - // 响应内容 + + // 统一输出响应内容 echo $response->content; } diff --git a/framework/library/exception/ResponseException.php b/framework/library/exception/ResponseException.php new file mode 100644 index 0000000..907504c --- /dev/null +++ b/framework/library/exception/ResponseException.php @@ -0,0 +1,19 @@ +header as $value) { header($value); } + return $this; } @@ -118,7 +119,7 @@ class Response 'Status ' . $text, ]); } - throw new Exception('不支持的状态码:' . $code); + throw new ResponseException('不支持的状态码:' . $code); } /** @@ -148,6 +149,23 @@ class Response } } + /** + * 输出文件 + * @param $filename + * @param $name + * @return Response + */ + public function sendFile($filename = null, $name = null) + { + if (is_file($filename)) { + $name = ($name) ? $name : uniqid() . '.' . substr($filename, strrpos($filename, '.') + 1); + return $this->header([ + 'Content-Disposition: attachment; filename="' . $name . '"', + ])->code(200)->send(readfile($filename)); + } + throw new ResponseException('不存在的文件:' . $filename); + } + /** * 处理数据 * @param $data @@ -173,13 +191,4 @@ class Response return $data; } - /** - * 直接echo处理 - * @return string - */ - public function __toString() - { - return $this->content; - } - }