如何使用typo3/extbase触发下载?
我将 Typo3 与 extbase 和 Fluid 一起使用。我有一个控制器,其操作名为 downloadAction()
。调用该操作后,系统尝试呈现下载模板(但我只想开始下载)。
public function downloadAction($id) {
// create file
// send header
// dump file
// exit
}
如何转储创建的下载文件并发送下载标头而不是正常的渲染过程?最好的方法是什么?
谢谢
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
几个月前我在一个项目中做到了这一点,它非常简单。
I did this in a project some months ago, it's pretty straight forward.
您可以为下载请求定义特殊的 PageType:
“efempty”必须替换为您的扩展名。
您现在可以像这样触发下载:
You can define a special PageType for download requests:
"efempty" must be replaced by your extension's name.
You are now able to trigger the download like this:
更新:
这实际上不适用于激活的压缩,因为内容长度仍代表未压缩的大小。 eID 可以用作解决方法,事实上甚至有一个官方调用:
eID = "dumpFile" 请参阅typo3/sysext/core/Resource/PHP/FileDumpEID.php
但是,此调用不会强制下载,而是“转储”它。我已经在 Forge 上提交了一张票来解决这个问题(该票已被接受并且针对 LTS 7): https: //forge.typo3.org/issues/67111
旧答案:
至于现在实现这一点的最简单方法是:
TypoScript Constants
TypoScript Setup
Controller Action
Fluid
与其他提到的答案相比的好处如下:
旁注:iPhone 忽略内容处置标题(始终内联)
关于“退出”问题,我还没有测试过它,但对于页面类型,如果您使用自己的 PHPView(StandaloneView?),它可能会起作用。
Update:
This actually doesn't work with activated compression because the Content-Length will still represent the uncompressed size. eID could be used as workaround in fact there is even a official call for that:
eID = "dumpFile" see typo3/sysext/core/Resource/PHP/FileDumpEID.php
However this call won't force a download but instead "dump" it. I've made a ticket @ Forge to fix this (which was accepted and is targeted for LTS 7): https://forge.typo3.org/issues/67111
Old answer:
As for now the easiest way to accomplish this is:
TypoScript Constants
TypoScript Setup
Controller Action
Fluid
The benefits vs the other mentioned answers are as following:
Side note: iPhone ignores the Content-Disposition headers (always inline)
About the "exit" concerns i've not tested it but with the page type it might work if you would use a own PHPView (StandaloneView?).
只要标头尚未发送,尽管您可能想要 检查输出缓冲 和 检查将发送的标头列表。
As long as headers haven't already been sent, there is nothing at all stopping you from emitting your headers, sending the content and actually calling
exit
, though you might want to check for output buffering and clear any open buffers before doing so. You might also want to inspect the list of headers that will be sent.