Syren7 / owncloudApiBundle

This Bundle allows you to manage owncloud users throug provisioning api and managing files through sabre dav
GNU General Public License v2.0
6 stars 4 forks source link

Downloading OC files #2

Open rdksgit opened 8 years ago

rdksgit commented 8 years ago

Hi,

First of all thanks for this great bundle, creating folders and displaying files works a treat

But, how do we get the users to download the files?, currently giving the owncloud url it asks for authentication, but i thought since auth details are already saved on to symfony it will just look for that

Any ideas?

Thanks Sameer

TuemmlerKon commented 8 years ago

Hi Sameer,

sorry for not responding that long time.

Your solution should be the use of getFile() method from filesystem service:

//within your controller let's use a method called downloadAction()

/**
 * @Route("/", name="SomeRouteName")
 */
public function downloadAction() {
    //get the filesystem service
    $fs = $this->get('syren7_owncloud.filesystem');
    //read file information from cloud
    $file = $fs->getFile('Some/Path/file.pdf');
    //create response and pass file content as data
    $response = new Response($file->read());
    /**
     * set content type to 'application/octet-stream' since some other mime types will be displayed in browser
     * like text or pdf (depends on installed browser extensions)
     */
    $response->headers->set('Content-Type', 'application/octet-stream');
    //set content disposition (default filename for file download)
    $response->headers->set('Content-Disposition', sprintf('attachment; filename="%s"', basename($file->getPath())));
    //return your newly created response
    return $response;
}

This should do the work for you. I'm going to extend the documentation for this case soon!

Regards Konstantin

Edit: corrected my answer since makeDisposition() doesn't work in this case. Also see my example.