如何从symfony中的公用文件夹下载pdf文件

问题描述

我正在使用dompdf从html模板生成发票,每个工作都完美完成,并且发票以绝对文件路径和文件名保存在数据库中,当我访问公用/发票时,我在那找到了我所有的pdf文件,我想单击链接按钮以下载或在浏览器中打开文件。

我创建了一个链接,该链接接受这样的绝对文件:

{% for invoice in invoices %}
<tr>
  <td> {{ invoice.id }} </td>
  <td>{{ invoice.fileName }}</td>
  <td>{{ invoice.booking.user.name }}</td>
  <td>{{ invoice.booking.car.registrationNumber }}</td>
  <td>{{ invoice.booking.startDate | date('d-m-Y') }}</td>
  <td><a href="" class="btn btn-sm btn-outline-danger">Archiver</a></td>
  <td><a href="{{ asset('invoices/' ~ invoice.filePath) }}) }}" style="color: #DDDDDD" class="btn btn-sm btn-dark"><i style="padding-right: 10px" class="fa fa-download"></i>Télecharger</a>
  </td>
</tr>
{% endfor %}

我收到此错误:

找不到“ GET”的路线 /home/sahnoun/Downloads/SousseCar/public/invoices/aKRyaziz.pdf)%20%7D%7D“ (来自“ http://127.0.0.1:8000/admin/invoice/all”)

解决方法

在尝试使用asset()函数时,您两次关闭了大括号,这会在输出中生成附加的“)}}”(URL编码:“)%20%7D%7D”,您可以看到在错误消息中)。很自然,这个路径不存在。

...
// remove surplus '}})'
href="{{ asset('invoices/' ~ invoice.filePath) }}) }}"
...

右:

href="{{ asset('invoices/' ~ invoice.filePath) }}"
,

实际上,您应该将发票信息公开给公共互联网-这本质上是巨大的安全性和用户信息泄漏-请将该逻辑移至控制器内部,以检查请求用户是否对此具有适当的权利信息,然后返回例如。 BinaryFileResponse来自该控制器。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...