Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: New method DownloadResponse::inline() #7207

Merged
merged 4 commits into from
Feb 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion system/HTTP/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
}
8 changes: 8 additions & 0 deletions tests/system/HTTP/DownloadResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions user_guide_src/source/changelogs/v4.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,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 <creating-a-layout>` for details.
- **Auto Routing (Improved)**: Now you can use URI without a method name like
``product/15`` where ``15`` is an arbitrary number.
Expand All @@ -70,6 +73,8 @@ Message Changes
Changes
*******

- **DownloadResponse:** When generating response headers, does not replace the ``Content-Disposition`` header if it was previously specified.

iRedds marked this conversation as resolved.
Show resolved Hide resolved
Deprecations
************

Expand Down
10 changes: 10 additions & 0 deletions user_guide_src/source/outgoing/response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ 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.

iRedds marked this conversation as resolved.
Show resolved Hide resolved
.. _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
``DownloadResponse::inline()`` method.

.. literalinclude:: response/028.php

HTTP Caching
============

Expand Down
6 changes: 6 additions & 0 deletions user_guide_src/source/outgoing/response/028.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php

$data = 'Here is some text!';
$name = 'mytext.txt';

return $this->response->download($name, $data)->inline();