Skip to content

Instantly share code, notes, and snippets.

@matej21
Created December 15, 2013 17:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matej21/7975975 to your computer and use it in GitHub Desktop.
Save matej21/7975975 to your computer and use it in GitHub Desktop.
<?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