Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a link that download a file in Symfony

I did some research and I found this post: How to create a link to download generated documents in symfony2?

I tried the solution, but it show my pdf in the browser, but what I want is that when someone click the link, it directly download the file. Is ther a way to do that with Symfony?

Kévin Duguay

like image 594
Kévin Duguay Avatar asked Mar 07 '15 00:03

Kévin Duguay


1 Answers

Set up an action. This uses annotation for the route. YOu can of course use yml or xml or whatever you are currently using

/**
 * @Route("/download", name="download_file")
**/
public function downloadFileAction(){
    $response = new BinaryFileResponse('path/to/pdf.pdf');
    $response->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT,'pdf.pdf');
    return $response;
}

Twig template:

<a href="{{path('download_file')}}">Download file</a>
like image 155
Kris Avatar answered Sep 23 '22 01:09

Kris