I use the VichUploaderBundle bundle on my Symfony 4 project for the attachment upload. The upload works well but I am bothered to display these attachments.
I use the plugin "ekko-lightbox" (or another plugin if you know better?), to display the attachments directly to the user without leaving the page but I get a lot of weird characters ���.
Here is my code (I use a controller to check that the user has the rights to download the file) :
/**
* @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);
}
Twig :
{% 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 %}
When I go directly to the route "/ticket/attachment/{id}", the attachment is displayed but with the ekko-lightbox plugin here is what I get (a lot of weird characters ���):
The problem was due to the plugin. I corrected the problem using the lokeshdhakar / lightbox2 plugin.