VichUploaderBundle - 使用控制器在 ekkoLightbox 中提供文件
VichUploaderBundle - Serving files in ekkoLightbox with a controller
我在我的 Symfony 4 项目中使用 VichUploaderBundle 包来上传附件。
上传效果很好,但我懒得显示这些附件。
我使用插件 "ekko-lightbox"(或者其他插件,如果你更了解?),在不离开页面的情况下直接向用户显示附件,但我得到了很多奇怪的字符 ����.
这是我的代码(我使用控制器检查用户是否有权下载文件):
/**
* @Route("/ticket/attachment/{id}", name="ticketing_attachments", methods="GET")
* @IsGranted("ROLE_USER")
*/
public function getTicketAttachment(TicketAttachment $ticketAttachment, DownloadHandler $downloadHandler, TicketManager $ticketManager): Response
{
if(!$ticketManager->hasPermissionViewTicket($ticketAttachment->getTicket(), $this->getUser())) {
throw new HttpException(404, 'La page que vous avez demandée n\'existe pas.', null, [], 1);
}
return $downloadHandler->downloadObject($ticketAttachment, $fileField = 'file', $objectClass = null, $fileName = null, $forceDownload = false);
}
树枝:
{% if ticket.attachments|length > 0 %}
<hr/>
{% for attachment in ticket.attachments %}
<span class="glyphicon glyphicon-paperclip"></span>
<a href="{{ path('ticketing_attachments', {id: attachment.id}) }}" data-toggle="lightbox">{{ attachment.name }}</a>
{% endfor %}
{% endif %}
当我直接转到路线“/ticket/attachment/{id}”时,显示附件,但是使用ekko-lightbox插件我得到的是这里(很多奇怪的字符����) :
ekko-lightbox 问题:
问题出在插件上。
我使用 lokeshdhakar / lightbox2 插件纠正了这个问题。
我在我的 Symfony 4 项目中使用 VichUploaderBundle 包来上传附件。 上传效果很好,但我懒得显示这些附件。
我使用插件 "ekko-lightbox"(或者其他插件,如果你更了解?),在不离开页面的情况下直接向用户显示附件,但我得到了很多奇怪的字符 ����.
这是我的代码(我使用控制器检查用户是否有权下载文件):
/**
* @Route("/ticket/attachment/{id}", name="ticketing_attachments", methods="GET")
* @IsGranted("ROLE_USER")
*/
public function getTicketAttachment(TicketAttachment $ticketAttachment, DownloadHandler $downloadHandler, TicketManager $ticketManager): Response
{
if(!$ticketManager->hasPermissionViewTicket($ticketAttachment->getTicket(), $this->getUser())) {
throw new HttpException(404, 'La page que vous avez demandée n\'existe pas.', null, [], 1);
}
return $downloadHandler->downloadObject($ticketAttachment, $fileField = 'file', $objectClass = null, $fileName = null, $forceDownload = false);
}
树枝:
{% if ticket.attachments|length > 0 %}
<hr/>
{% for attachment in ticket.attachments %}
<span class="glyphicon glyphicon-paperclip"></span>
<a href="{{ path('ticketing_attachments', {id: attachment.id}) }}" data-toggle="lightbox">{{ attachment.name }}</a>
{% endfor %}
{% endif %}
当我直接转到路线“/ticket/attachment/{id}”时,显示附件,但是使用ekko-lightbox插件我得到的是这里(很多奇怪的字符����) :
ekko-lightbox 问题:
问题出在插件上。 我使用 lokeshdhakar / lightbox2 插件纠正了这个问题。