Skip to content

Commit

Permalink
Merge pull request #2129 from MGatner/download-setfilename
Browse files Browse the repository at this point in the history
Add setFileName() to DownloadResponse
  • Loading branch information
lonnieezell authored Aug 13, 2019
2 parents 013b6e9 + 7aa6f34 commit d2f2ada
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
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

0 comments on commit d2f2ada

Please sign in to comment.