我制作了一个表单来将文件上传到文件夹./data/uploads
使用Zend\Filter\File\RenameUpload
filter.
这就像一个魅力。我现在的问题是如何提供这个文件给用户下载呢?
我认为它会是这样的:
$response->setContent(file_get_contents('./data/uploads/file.png'));
但我想知道最好的方法是什么。
对于任何遇到此线程寻找答案的人来说,这是一个有效的解决方案,并且它使用流!
public function downloadAction() {
$fileName = 'somefile';
$response = new \Zend\Http\Response\Stream();
$response->setStream(fopen($fileName, 'r'));
$response->setStatusCode(200);
$headers = new \Zend\Http\Headers();
$headers->addHeaderLine('Content-Type', 'whatever your content type is')
->addHeaderLine('Content-Disposition', 'attachment; filename="' . $fileName . '"')
->addHeaderLine('Content-Length', filesize($fileName));
$response->setHeaders($headers);
return $response;
}
在这里找到:使用 zf2 强制下载 https://stackoverflow.com/questions/15219873/force-download-using-zf2
更多详细信息请参见此处:使用 zend 发送流响应 http://www.sasaprolic.com/2013/02/sending-stream-responses-with-zend.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)