Monday, March 4, 2013

Cakephp 2.3.x Sending Files and forcing the download of a mp4 file

I'm using cakephp 2.3.1

I want to force download a mp4 file per http://book.cakephp.org/2.0/en/controllers/request-response.html#cake-response-file

In my 'view' I have the following code which is correctly searching for the filename, and finding the file name, and displaying the download link:

<?php $filename = APP . 'webroot/files/' . $dance['Dance']['id'] . '.mp4';   if (file_exists($filename)) {      echo $this->Html->link('DOWNLOAD', array('controller' => 'dances', 'action' => 'sendFile', $dance['Dance']['id']));       } else {      echo 'Coming soon: available April 16th';      }  ?>

When the user clicks on the link I want to force the download of the mp4 file. In my controller I have the following code that doesn't work:

public function sendFile($id) {      $file = $this->Attachment->getFile($id); //Note: I do not understand the 'Attachment' and the 'getFile($id)'      $this->response->file($file['webroot/files/'], array('download' => true, 'name' => 'Dance'));      //Return reponse object to prevent controller from trying to render a view      return $this->response;  }   

I don't understand 'Attachment' and the 'getFile()'

I'm getting the following error: Error: Call to a member function getFile() on a non-object

What am I doing wrong and is there any other documentation I can be looking at to understand this better?


--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

No comments: