From 89f499e54cd952dde81b669c439ae4fb534325b0 Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Wed, 1 Feb 2023 09:04:08 +0800 Subject: [PATCH 1/4] Feature: New method DownloadResponse::inline() --- system/HTTP/DownloadResponse.php | 17 ++++++++++++++++- tests/system/HTTP/DownloadResponseTest.php | 8 ++++++++ user_guide_src/source/changelogs/v4.4.0.rst | 4 ++++ user_guide_src/source/outgoing/response.rst | 8 ++++++++ user_guide_src/source/outgoing/response/028.php | 6 ++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 user_guide_src/source/outgoing/response/028.php diff --git a/system/HTTP/DownloadResponse.php b/system/HTTP/DownloadResponse.php index 899bec8583c6..022f12560f5c 100644 --- a/system/HTTP/DownloadResponse.php +++ b/system/HTTP/DownloadResponse.php @@ -269,7 +269,10 @@ public function buildHeaders() $this->setContentTypeByMimeType(); } - $this->setHeader('Content-Disposition', $this->getContentDisposition()); + if (! $this->hasHeader('Content-Disposition')) { + $this->setHeader('Content-Disposition', $this->getContentDisposition()); + } + $this->setHeader('Expires-Disposition', '0'); $this->setHeader('Content-Transfer-Encoding', 'binary'); $this->setHeader('Content-Length', (string) $this->getContentLength()); @@ -325,4 +328,16 @@ private function sendBodyByBinary() return $this; } + + /** + * Sets the response header to display the file in the browser. + * + * @return DownloadResponse + */ + public function inline() + { + $this->setHeader('Content-Disposition', 'inline'); + + return $this; + } } diff --git a/tests/system/HTTP/DownloadResponseTest.php b/tests/system/HTTP/DownloadResponseTest.php index fe320705b290..7068434a6fda 100644 --- a/tests/system/HTTP/DownloadResponseTest.php +++ b/tests/system/HTTP/DownloadResponseTest.php @@ -120,6 +120,14 @@ public function testSetFileName() $this->assertSame('attachment; filename="myFile.txt"; filename*=UTF-8\'\'myFile.txt', $response->getHeaderLine('Content-Disposition')); } + public function testDispositionInline(): void + { + $response = new DownloadResponse('unit-test.txt', true); + $response->inline(); + $response->buildHeaders(); + $this->assertSame('inline', $response->getHeaderLine('Content-Disposition')); + } + public function testNoCache() { $response = new DownloadResponse('unit-test.txt', true); diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index 748829333165..1f1705dbd877 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -28,6 +28,8 @@ Method Signature Changes Enhancements ************ +- Added ``DownloadResponse::inline()`` method that sets the ``Content-Disposition: inline`` header to display the file +in the browser. Commands ======== @@ -69,6 +71,8 @@ Message Changes Changes ******* +- The ``DownloadResponse`` class, when generating response headers, does not replace the ``Content-Disposition`` header +if it was previously specified. Deprecations ************ diff --git a/user_guide_src/source/outgoing/response.rst b/user_guide_src/source/outgoing/response.rst index c3c10bc8d40d..e4fabdf8d451 100644 --- a/user_guide_src/source/outgoing/response.rst +++ b/user_guide_src/source/outgoing/response.rst @@ -93,6 +93,14 @@ Use the optional ``setFileName()`` method to change the filename as it is sent t .. note:: The response object MUST be returned for the download to be sent to the client. This allows the response to be passed through all **after** filters before being sent to the client. +Open file in browser +-------------------- + +Some browsers can display files such as PDF. To tell the browser to display the file instead of saving it, call the +``DownloadResponse::inline()`` method. + +.. literalinclude:: response/007.php + HTTP Caching ============ diff --git a/user_guide_src/source/outgoing/response/028.php b/user_guide_src/source/outgoing/response/028.php new file mode 100644 index 000000000000..8afaf84960ab --- /dev/null +++ b/user_guide_src/source/outgoing/response/028.php @@ -0,0 +1,6 @@ +response->download($name, $data)->inline(); From 2f5980922257da10db9843db25d25986223d83bb Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Wed, 1 Feb 2023 09:22:07 +0800 Subject: [PATCH 2/4] fix doc --- user_guide_src/source/changelogs/v4.4.0.rst | 6 ++++-- user_guide_src/source/outgoing/response.rst | 2 +- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index 1f1705dbd877..bfcd77504631 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -28,8 +28,9 @@ Method Signature Changes Enhancements ************ -- Added ``DownloadResponse::inline()`` method that sets the ``Content-Disposition: inline`` header to display the file -in the browser. + +- Added ``DownloadResponse::inline()`` method that sets the ``Content-Disposition: inline`` header to display the +file in the browser. Commands ======== @@ -71,6 +72,7 @@ Message Changes Changes ******* + - The ``DownloadResponse`` class, when generating response headers, does not replace the ``Content-Disposition`` header if it was previously specified. diff --git a/user_guide_src/source/outgoing/response.rst b/user_guide_src/source/outgoing/response.rst index e4fabdf8d451..914c0f94734d 100644 --- a/user_guide_src/source/outgoing/response.rst +++ b/user_guide_src/source/outgoing/response.rst @@ -99,7 +99,7 @@ Open file in browser Some browsers can display files such as PDF. To tell the browser to display the file instead of saving it, call the ``DownloadResponse::inline()`` method. -.. literalinclude:: response/007.php +.. literalinclude:: response/028.php HTTP Caching ============ From c8795464108db008466e8cbc453caef032c5cf7d Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Wed, 1 Feb 2023 09:37:25 +0800 Subject: [PATCH 3/4] fix doc2 --- user_guide_src/source/changelogs/v4.4.0.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index bfcd77504631..8d46a98b9cff 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -29,8 +29,7 @@ Method Signature Changes Enhancements ************ -- Added ``DownloadResponse::inline()`` method that sets the ``Content-Disposition: inline`` header to display the -file in the browser. +- Added ``DownloadResponse::inline()`` method that sets the ``Content-Disposition: inline`` header to display the file in the browser. Commands ======== @@ -73,8 +72,7 @@ Message Changes Changes ******* -- The ``DownloadResponse`` class, when generating response headers, does not replace the ``Content-Disposition`` header -if it was previously specified. +- The ``DownloadResponse`` class, when generating response headers, does not replace the ``Content-Disposition`` header if it was previously specified. Deprecations ************ From 6469b0704597b4c2883469fa9d94b369ec929876 Mon Sep 17 00:00:00 2001 From: Andrey Pyzhikov <5071@mail.ru> Date: Sat, 4 Feb 2023 07:52:20 +0800 Subject: [PATCH 4/4] Documentation and changelog changes --- user_guide_src/source/changelogs/v4.4.0.rst | 7 ++++--- user_guide_src/source/outgoing/response.rst | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/user_guide_src/source/changelogs/v4.4.0.rst b/user_guide_src/source/changelogs/v4.4.0.rst index 8d46a98b9cff..59321b3d0b54 100644 --- a/user_guide_src/source/changelogs/v4.4.0.rst +++ b/user_guide_src/source/changelogs/v4.4.0.rst @@ -29,8 +29,6 @@ Method Signature Changes Enhancements ************ -- Added ``DownloadResponse::inline()`` method that sets the ``Content-Disposition: inline`` header to display the file in the browser. - Commands ======== @@ -61,6 +59,9 @@ Helpers and Functions Others ====== +- **DownloadResponse:** Added ``DownloadResponse::inline()`` method that sets + the ``Content-Disposition: inline`` header to display the file in the browser. + See :ref:`open-file-in-browser` for details. - **View:** Added optional 2nd parameter ``$saveData`` on ``renderSection()`` to prevent from auto cleans the data after displaying. See :ref:`View Layouts ` for details. - **Auto Routing (Improved)**: Now you can use URI without a method name like ``product/15`` where ``15`` is an arbitrary number. @@ -72,7 +73,7 @@ Message Changes Changes ******* -- The ``DownloadResponse`` class, when generating response headers, does not replace the ``Content-Disposition`` header if it was previously specified. +- **DownloadResponse:** When generating response headers, does not replace the ``Content-Disposition`` header if it was previously specified. Deprecations ************ diff --git a/user_guide_src/source/outgoing/response.rst b/user_guide_src/source/outgoing/response.rst index 914c0f94734d..3f435702c34d 100644 --- a/user_guide_src/source/outgoing/response.rst +++ b/user_guide_src/source/outgoing/response.rst @@ -93,7 +93,9 @@ Use the optional ``setFileName()`` method to change the filename as it is sent t .. note:: The response object MUST be returned for the download to be sent to the client. This allows the response to be passed through all **after** filters before being sent to the client. -Open file in browser +.. _open-file-in-browser: + +Open File in Browser -------------------- Some browsers can display files such as PDF. To tell the browser to display the file instead of saving it, call the