php – Symfony 1.4:最好的方法是在不使用模板/视图的情况下提
发布时间:2020-12-13 17:43:49 所属栏目:PHP教程 来源:网络整理
导读:对于cource,其他一些人已经在stackoverflow上讨论过这些问题,但并非所有的ansers都适用于我,并且通常他们不提供symfony安装的版本. 我读过的主题: Send attachment/Download file from Symfony action How to download a file on clicking thefile path usi
对于cource,其他一些人已经在stackoverflow上讨论过这些问题,但并非所有的ansers都适用于我,并且通常他们不提供symfony安装的版本.
我读过的主题: > Send attachment/Download file from Symfony action 这就是我要问你如何处理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(); 这没有模板文件.但imho这不是那么漂亮的编码. 模板的替代方法: 控制器: /** @var $response sfWebResponse */ $response = $this->getResponse(); $response->clearHttpHeaders(); $response->setContentType($mimeType); $response->setHttpHeader('Content-Disposition','public'); $response->setContent(file_get_contents($filePath)); $response->sendHttpHeaders(); return sfView::NONE; 视图: <?php echo $sf_response->getRawValue()->getContent(); ?> 解决方法
我的首选解决方案
$filePath = $document->getAbsoluteFilePath(); $mimeType = mime_content_type($filePath); /** @var $response sfWebResponse */ $response = $this->getResponse(); $response->clearHttpHeaders(); $response->setContentType($mimeType); $response->setHttpHeader('Content-Disposition',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)); 无需使用模板文件.使用symfony标准行为.重要提示:模板文件必须存在! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |