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.
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:
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->_helper->Layout()->disableLayout();
$this->_helper->ViewRenderer->setNeverRender();
$this->getResponse()
->clearAllHeaders()
->clearBody()
->setHttpResponseCode(200)
->setHeader('Content-Type', 'image/jpeg')
->setHeader('Content-Length', strlen($dokument->Dokument))
->setHeader(
'Content-Disposition',
"attachment;filename=\"{$dokument->filename}\""
)
->setHeader('Last-Modified', $dokument->lastModified)
->setBody($dokument->Dokument);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.