Commit a3e7a33a by Qiang Xue

Merge pull request #4748 from klimov-paul/4424-x-send-file

Enh #4424: Added `forceDownload` parameter to `yii\web\Response::xSendFile()`
parents a56490e0 fe36bc9e
......@@ -173,6 +173,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)
......
......@@ -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.
......@@ -632,10 +632,12 @@ 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 string $xHeader the name of the x-sendfile header.
* @param array $options additional options. Valid options are:
* - forceDownload: boolean, whether the file will be downloaded or shown inline. Defaults to true.
* - xHeader: string, the name of the x-sendfile header. Defaults to "X-Sendfile".
* @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, $options = [])
{
if ($mimeType === null && ($mimeType = FileHelper::getMimeTypeByExtension($filePath)) === null) {
$mimeType = 'application/octet-stream';
......@@ -643,11 +645,21 @@ class Response extends \yii\base\Response
if ($attachmentName === null) {
$attachmentName = basename($filePath);
}
if (isset($options['xHeader'])) {
$xHeader = $options['xHeader'];
} else {
$xHeader = 'X-Sendfile';
}
if (!isset($options['forceDownload']) || $options['forceDownload']) {
$disposition = 'attachment';
} else {
$disposition = 'inline';
}
$this->getHeaders()
->setDefault($xHeader, $filePath)
->setDefault('Content-Type', $mimeType)
->setDefault('Content-Disposition', "attachment; filename=\"$attachmentName\"");
->setDefault('Content-Disposition', "{$disposition}; filename=\"{$attachmentName}\"");
return $this;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment