Wednesday, February 25, 2009

Media View not downloading unless I set headers

I am unable to get the Media View to force the download to start, no
error is given other than it just lands on a blank page. Here's the
download function in my controller;

function download ($id = null) {
list($file_name, $file_ext) = split('[.]', $id);
$this->view = 'Media';
$params = array(
'id' => $file_name . '.' . $file_ext,
'name' => $file_name,
'download' => true,
'extension' => 'epub',
'path' => 'app' . DS . 'files' . DS,
'mimeType', array('epub' => 'application/epub+zip'
));
$this->set($params);
}

Like I say, no download (I tried with an without the 'app'.DS on the
path) but if I send the headers like in this post then it works;

http://groups.google.com/group/cake-php/browse_thread/thread/cf567fbb9beccfcb/b69133b6c7d70fa6?hl=en&lnk=gst&q=how+to+have+file+downloads#b69133b6c7d70fa6

So, I replace $this->set($params) with;

if(!empty($id)){
$file = $params['path'] . $params['id'];
$data = file_get_contents($file);
$size = filesize($file);
if( isset($_SERVER['HTTP_USER_AGENT']) && strpos($_SERVER
['HTTP_USER_AGENT'], 'MSIE') ) {
header('Content-Type:application/force-download');
} else {
header('Content-Type:application/epub+zip');
header('Content-Length:' . $size);
header('Content-disposition:attachment;filename="' . $params
['id'] . '"');
echo $data;
}
} else {
$this->redirect();
exit();
}

I guess that Media View was implemented so we don't have to send the
headers ourselves - or am I am wrong?

Does anyone have any ideas why the default Model View method is not
working?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: