fal*_*sch 4 php symfony1 symfony-1.4
对于cource,其他一些人已经在stackoverflow上讨论过这些问题,但并非所有的ansers都适合我,并且通常他们不提供symfony安装的版本.
我读过的主题:
这就是我要问你如何处理symfony 1.4中的文件下载(不使用视图)的问题?在我的所有用例中,我需要一个模板文件来呈现响应.如果我发送响应由于控制器有唯一的可能性发送它没有PHP错误(标头已发送)与
控制器:
/** @var $response sfWebResponse */
$response = $this->getResponse();
$response->clearHttpHeaders();
$response->setContentType($mimeType);
$response->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($filePath) . '"');
$response->setHttpHeader('Content-Description', 'File Transfer');
$response->setHttpHeader('Content-Transfer-Encoding', 'binary');
$response->setHttpHeader('Content-Length', filesize($filePath));
$response->setHttpHeader('Cache-Control', 'public, must-revalidate');
$response->setHttpHeader('Pragma', 'public');
$response->sendHttpHeaders();
readfile($filePath); die();
Run Code Online (Sandbox Code Playgroud)
这没有模板文件.但imho这不是那么漂亮的编码.
模板的替代方法:
控制器:
/** @var $response sfWebResponse */
$response = $this->getResponse();
$response->clearHttpHeaders();
$response->setContentType($mimeType);
$response->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($filePath) . '"');
$response->setHttpHeader('Content-Description', 'File Transfer');
$response->setHttpHeader('Content-Transfer-Encoding', 'binary');
$response->setHttpHeader('Content-Length', filesize($filePath));
$response->setHttpHeader('Cache-Control', 'public, must-revalidate');
$response->setHttpHeader('Pragma', 'public');
$response->setContent(file_get_contents($filePath));
$response->sendHttpHeaders();
return sfView::NONE;
Run Code Online (Sandbox Code Playgroud)
视图:
<?php echo $sf_response->getRawValue()->getContent(); ?>
Run Code Online (Sandbox Code Playgroud)
我的首选解决方案
$filePath = $document->getAbsoluteFilePath();
$mimeType = mime_content_type($filePath);
/** @var $response sfWebResponse */
$response = $this->getResponse();
$response->clearHttpHeaders();
$response->setContentType($mimeType);
$response->setHttpHeader('Content-Disposition', 'attachment; filename="' . basename($filePath) . '"');
$response->setHttpHeader('Content-Description', 'File Transfer');
$response->setHttpHeader('Content-Transfer-Encoding', 'binary');
$response->setHttpHeader('Content-Length', filesize($filePath));
$response->setHttpHeader('Cache-Control', 'public, must-revalidate');
// if https then always give a Pragma header like this to overwrite the "pragma: no-cache" header which
// will hint IE8 from caching the file during download and leads to a download error!!!
$response->setHttpHeader('Pragma', 'public');
//$response->setContent(file_get_contents($filePath)); # will produce a memory limit exhausted error
$response->sendHttpHeaders();
ob_end_flush();
return $this->renderText(readfile($filePath));
Run Code Online (Sandbox Code Playgroud)
无需使用模板文件.使用symfony标准行为.重要提示:模板文件必须存在!
归档时间: |
|
查看次数: |
9057 次 |
最近记录: |