问题
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
回答1:
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>
来源:https://stackoverflow.com/questions/28910112/how-to-create-a-link-that-download-a-file-in-symfony