从 zend 框架中的 mssql 数据库下载 blob(类型:图像)
我该怎么做?我尝试使用 file_put_contents(),但它创建了一个损坏的文件。
我现在正在尝试设置一个具有正确标题的视图以使其正常工作。
<?php
header('Content-Type: image/jpeg');
header('Content-Transfer-Encoding: base64');
echo $this->dokument->Dokument;
这是我的控制器的内容:
public function imageAction()
{
$id = $this->_getParam('id');
$dokumentTabell = new Ordre_Model_Table_OrdreDokument();
$dokument = $dokumentTabell->find($id)->current();
$this->view->dokument = $dokument;
// disable layout and view
$this->view->layout()->disableLayout();
}
根据此处的第一个答案对其进行编辑后,它给出以下错误消息: PHP Fatal error: Call to a member function setHttpResponseCode() on a non-object in ...\application\modules\第 45 行的 ordre\controllers\OrdreController.php
第 45 行是这样的:
->setHttpResponseCode(200)
代码的其余部分:
public function imageAction()
{
$id = $this->_getParam('id');
$dokumentTabell = new Ordre_Model_Table_OrdreDokument();
$dokument = $dokumentTabell->find($id)->current();
$this->view->dokument = $dokument;
$filename = $dokument->Name.".".$dokument->FileExtension;
// disable layout and view
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$this->getResponse()
->clearAllHeaders()
->clearBody()
->setHttpResponseCode(200)
->setHeader('Content-Type', 'image/jpeg')
->setHeader('Content-Length', strlen($dokument->Dokument))
->setHeader(
'Content-Disposition',
"attachment;filename=\"{$filename}\""
)
->setHeader('Last-Modified', $dokument->SistEndret)
->setBody($dokument->Dokument);
}
如果我删除第 44 行: ->clearBody(),则代码“有效”。我得到一个名称正确且大小大致正确的文件。
How do I do it? I've tried to use file_put_contents(), but it created a corrupted file.
I'm now trying to set up a view with the correct headers to get this working.
<?php
header('Content-Type: image/jpeg');
header('Content-Transfer-Encoding: base64');
echo $this->dokument->Dokument;
this is the content of my controller:
public function imageAction()
{
$id = $this->_getParam('id');
$dokumentTabell = new Ordre_Model_Table_OrdreDokument();
$dokument = $dokumentTabell->find($id)->current();
$this->view->dokument = $dokument;
// disable layout and view
$this->view->layout()->disableLayout();
}
After editing it according to the first answer here, it gives this error message: PHP Fatal error: Call to a member function setHttpResponseCode() on a non-object in ...\application\modules\ordre\controllers\OrdreController.php on line 45
line 45 is this:
->setHttpResponseCode(200)
The rest of the code:
public function imageAction()
{
$id = $this->_getParam('id');
$dokumentTabell = new Ordre_Model_Table_OrdreDokument();
$dokument = $dokumentTabell->find($id)->current();
$this->view->dokument = $dokument;
$filename = $dokument->Name.".".$dokument->FileExtension;
// disable layout and view
$this->view->layout()->disableLayout();
$this->_helper->viewRenderer->setNoRender(true);
$this->getResponse()
->clearAllHeaders()
->clearBody()
->setHttpResponseCode(200)
->setHeader('Content-Type', 'image/jpeg')
->setHeader('Content-Length', strlen($dokument->Dokument))
->setHeader(
'Content-Disposition',
"attachment;filename=\"{$filename}\""
)
->setHeader('Last-Modified', $dokument->SistEndret)
->setBody($dokument->Dokument);
}
If I remove line 44: ->clearBody(), the code "works". I get a file with the right name and approximately the right size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,视图永远不应该输出标题。对我来说,这感觉更像是控制器要做的事情。此外,在控制器中,您可以访问响应对象,该对象具有设置标头的方法。
另外,
$dokument->Dokument
是base64编码的吗?否则,只需跳过该标头。我可能会做类似的事情:
In my opinion, views should never output headers.. To me, it feels much more like a controller thing to do. Also, in the controller you have access to the response object, which has methods for setting headers.
Also, is
$dokument->Dokument
base64-encoded? Otherwise, just skip that header.I'd probably do something like: