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

Add setFileName() to DownloadResponse #2129

Merged
merged 3 commits into from
Aug 13, 2019
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
13 changes: 13 additions & 0 deletions system/HTTP/DownloadResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,19 @@ public function setFilePath(string $filepath)
$this->file = new File($filepath, true);
}

/**
* set name for the download.
*
* @param string $filename
*
* @return $this
*/
public function setFileName(string $filename)
{
$this->filename = $filename;
return $this;
}

/**
* get content length.
*
Expand Down
9 changes: 9 additions & 0 deletions tests/system/HTTP/DownloadResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ public function testSetContentTypeNoCharSet()

$this->assertEquals('application/octet-stream', $response->getHeaderLine('Content-Type'));
}

public function testSetFileName()
{
$response = new DownloadResponse('unit-test.txt', true);
$response->setFileName('myFile.txt');
$response->buildHeaders();

$this->assertSame('attachment; filename="myFile.txt"; filename*=UTF-8\'\'myFile.txt', $response->getHeaderLine('Content-Disposition'));
}

public function testNoCache()
{
Expand Down
4 changes: 4 additions & 0 deletions user_guide_src/source/outgoing/response.rst
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ do the following::
// Contents of photo.jpg will be automatically read
return $response->download('/path/to/photo.jpg', NULL);

Use the optional ``setFileName()`` method to change the filename as it is sent to the client's browser::

return $response->download('awkwardEncryptedFileName.fakeExt')->setFileName('expenses.csv');

.. 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.

Expand Down