gpt4 book ai didi

php - 交响乐 1.4 : Best way provide a file download without using a template/view

转载 作者:可可西里 更新时间:2023-10-31 22:13:03 24 4
gpt4 key购买 nike

当然,其他一些人已经在 stackoverflow 上讨论了这些问题,但并非所有答案都适合我,而且他们通常不提供 symfony 安装的版本。

我阅读的主题:

这就是我要问您如何在 symfony 1.4 中处理文件下载(不使用 View )的目的?在我所有的用例中,我都需要一个模板文件来呈现响应。如果我由于 Controller 发送响应,则唯一的可能性是发送它而不会出现 php 错误( header 已发送)

Controller :

/** @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();

这在没有模板文件的情况下工作。但是恕我直言,这不是很好的编码。

模板的另一种方式:

Controller :

 /** @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;

View :

<?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', '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));

无需使用模板文件。 使用 symfony 标准行为。重要提示:模板文件必须存在!

关于php - 交响乐 1.4 : Best way provide a file download without using a template/view,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14142670/

24 4 0