I am trying to download a file in ZF2 & stuck somewhere. I am not able to get the solution. The file is proper & readable. I think there's a problem in the header settings. Please help!. The excerpt code is as follows:
$response = new \Zend\Http\Response\Stream();
$response->setStream(
fopen($filePath, 'r')
);
$response->setStatusCode(200);
$headers = new \Zend\Http\Headers();
$headers->addHeaderLine('Content-Type', 'application/octet-stream');
$headers->addHeaderLine('Content-Transfer-Encoding','binary');
$headers->addHeaderLine('Content-Type', 'application/octet-stream');
$headers->addHeaderLine(
'Content-Disposition', 'attachment; filename="' . $media->getName() . '"'
)
->addHeaderLine(
'Content-Length', filesize($filePath)
);
$response->setHeaders($headers);
return $response;
I have referred to the following: force download using ZF2 & Zend Framework 2 - Rendering CSV data
I am getting a corrupted file