如何创建一个链接来下载Symfony中的文件 [英] How to create a link that download a file in Symfony

查看:39
本文介绍了如何创建一个链接来下载Symfony中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了一些研究,发现了这篇文章:如何创建链接以下载生成的文档symfony2?

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

我尝试了该解决方案,但是它在浏览器中显示了我的pdf,但是我想要的是当有人单击链接时,它将直接下载文件.这是使用Symfony做到这一点的一种方法吗?

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évinDuguay)

Kévin Duguay

推荐答案

设置操作.这对路线使用注释.您当然可以使用yml或xml或您当前正在使用的任何内容

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;
}

树枝模板:

<a href="{{path('download_file')}}">Download file</a>

这篇关于如何创建一个链接来下载Symfony中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆