Created
December 15, 2013 17:56
-
-
Save matej21/7975975 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ContentResponse extends \Nette\Object implements \Nette\Application\IResponse | |
{ | |
protected $forDownload = FALSE; | |
/** @var string */ | |
private $file; | |
/** @var string */ | |
private $contentType; | |
/** @var string */ | |
private $name; | |
/** | |
* @param mixed $file file content | |
* @param string $name imposed file name | |
* @param boolean $forDownload is file for download? | |
* @param string $contentType MIME content type | |
*/ | |
public function __construct($file, $name = NULL, $forDownload = FALSE, $contentType = NULL) | |
{ | |
$this->file = $file; | |
$this->name = $name ? $name : "file"; | |
$this->contentType = $contentType ? $contentType : 'application/octet-stream'; | |
$this->forDownload = $forDownload; | |
} | |
/** | |
* Returns the file name. | |
* | |
* @return string | |
*/ | |
final public function getName() | |
{ | |
return $this->name; | |
} | |
/** | |
* Returns the MIME content type of a downloaded file. | |
* | |
* @return string | |
*/ | |
final public function getContentType() | |
{ | |
return $this->contentType; | |
} | |
/** | |
* sends content to output | |
* | |
* @param Nette\Http\IRequest $httpRequest | |
* @param Nette\Http\IResponse $httpResponse | |
*/ | |
public function send(\Nette\Http\IRequest $httpRequest, \Nette\Http\IResponse $httpResponse) | |
{ | |
$httpResponse->setContentType($this->contentType); | |
$httpResponse->setHeader('Content-Disposition', ($this->forDownload ? 'attachment; ' : '') . 'filename="' . $this->name . '"'); | |
$filesize = strlen($this->file); | |
$httpResponse->setHeader('Content-Length', $filesize); | |
echo $this->file; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment