Reputation: 2519
Using Symfony 2.5 here, users upload MS Office files into our application and download it later as needed. Now, when the file attachment contains non-ascii chars (which is quite common as we are from Czech republic) Symfony raises error 'The filename fallback must only contain ASCII characters.'
I found many reports of this problem and discussions e.g.
... but no real solution. I know I can convert filename to ascci when making Content-Disposition header but it changes the filename presented to user which is not nice and quite misleading for user. Is there way how to avoid this and be able to serve former file name? Being able to download file with non-ascii chars in filename is quite common on internet so why this restriction?
By following this How to encode the filename parameter of Content-Disposition header in HTTP? I even tried to encode filename with urlencode() but now it says the % is not allowed char :-(
Update 1: Here is a code snippet I'm using. I'm using streaming the response to browser and headers are defined rather manualy I think.
$response = new StreamedResponse();
$response->headers->set('Content-Type', $upload->getMimeType());
$contentDisposition = $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $upload->getFilename());
$response->headers->set('Content-Disposition', $contentDisposition);
Upvotes: 5
Views: 6555
Reputation: 6786
Symfony 4 has $filenameFallback
in HeaderUtils::makeDisposition
.
Example
$filenameFallback = preg_replace(
'#^.*\.#',
md5($filename) . '.', $filename
);
$disposition = $response->headers->makeDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$filename,
$filenameFallback
);
$response->headers->set('Content-Disposition', $disposition);
Upvotes: 6