From e3cea2138a844bbe942aeaf544aaddb9985773cd Mon Sep 17 00:00:00 2001 From: Klimov Paul Date: Tue, 19 Aug 2014 12:18:35 +0300 Subject: [PATCH] Added `forceDownload` parameter to `yii\web\Response::xSendFile()` --- framework/CHANGELOG.md | 1 + framework/UPGRADE.md | 3 +++ framework/web/Response.php | 6 ++++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 0d641d5..ff59bd3 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -172,6 +172,7 @@ Yii Framework 2 Change Log - Enh #4317: Added `absoluteAuthTimeout` to yii\web\User (ivokund, nkovacs) - Enh #4360: Added client validation support for file validator (Skysplit) - Enh #4372: `yii\filters\HttpCache` failed to comply to RFC 7232 (DaSourcerer) +- Enh #4424: Added `forceDownload` parameter to `yii\web\Response::xSendFile()` (klimov-paul) - Enh #4436: Added callback functions to AJAX-based form validation (thiagotalma) - Enh #4485: Added support for deferred validation in `ActiveForm` (Alex-Code) - Enh #4520: Added sasl support to `yii\caching\MemCache` (xjflyttp) diff --git a/framework/UPGRADE.md b/framework/UPGRADE.md index db166f2..dcffed9 100644 --- a/framework/UPGRADE.md +++ b/framework/UPGRADE.md @@ -196,3 +196,6 @@ new ones save the following code as `convert.php` that should be placed in the s * The format of the Faker fixture template is changed. For an example, please refer to the file `apps/advanced/common/tests/templates/fixtures/user.php`. + +* Signature of the `yii\web\Response::xSendFile()` method has changed. If you're using or overriding this method you + must change your code to fit it. diff --git a/framework/web/Response.php b/framework/web/Response.php index f46a1ec..91a254a 100644 --- a/framework/web/Response.php +++ b/framework/web/Response.php @@ -632,10 +632,11 @@ class Response extends \yii\base\Response * @param string $filePath file name with full path * @param string $attachmentName file name shown to the user. If null, it will be determined from `$filePath`. * @param string $mimeType the MIME type of the file. If null, it will be determined based on `$filePath`. + * @param boolean $forceDownload whether the file will be downloaded or shown inline. * @param string $xHeader the name of the x-sendfile header. * @return static the response object itself */ - public function xSendFile($filePath, $attachmentName = null, $mimeType = null, $xHeader = 'X-Sendfile') + public function xSendFile($filePath, $attachmentName = null, $mimeType = null, $forceDownload = true, $xHeader = 'X-Sendfile') { if ($mimeType === null && ($mimeType = FileHelper::getMimeTypeByExtension($filePath)) === null) { $mimeType = 'application/octet-stream'; @@ -643,11 +644,12 @@ class Response extends \yii\base\Response if ($attachmentName === null) { $attachmentName = basename($filePath); } + $disposition = $forceDownload ? 'attachment' : 'inline'; $this->getHeaders() ->setDefault($xHeader, $filePath) ->setDefault('Content-Type', $mimeType) - ->setDefault('Content-Disposition', "attachment; filename=\"$attachmentName\""); + ->setDefault('Content-Disposition', "{$disposition}; filename=\"{$attachmentName}\""); return $this; }