I'm using this code to prompt for a file download on Symfony 2.7. It's working on Windows/Mac/iPhone, but not on Android:
if (!file_exists($path)) {
throw $this->createNotFoundException('File not found.');
}
$response = new BinaryFileResponse($path);
$response->trustXSendfileTypeHeader();
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
$name,
iconv('UTF-8', 'ASCII//TRANSLIT', $name)
);
When tapping the link, the download starts but then the error appears: Filename.pdf is of invalid format. If I press the link and select "Save Link", then the PDF si downloaded correctly and I can open it.
Does anyone know how to fix the download on Android? (tested using version 5).
Thank you
The code it's not the problem. I changed the file extension to uppercase and now it's working on Android: filename.pdf -> filename.PDF Stupid, I know.
Have you tried something like this:
$response->headers->set('Content-Type', 'application/pdf');
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'filename.pdf'
);
Don't know what your PDF would be referred to. Essentially adding the 'Content-Type' headers of PDF to see if that makes a difference. I'm not certain if this will work.
Edit #2 I saw this link: http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
So try this header change:
$response->headers->set('Content-Type', 'octet-stream');
$response->setContentDisposition(
ResponseHeaderBag::DISPOSITION_ATTACHMENT,
'filename.pdf'
);
Try it, and see if that changes the result.
Actually, thanks for your answer. I searched and found this:
http://www.digiblog.de/2011/04/android-and-the-download-file-headers/
Looks like there is a problem with older versions of Android. Maybe in your code you can check for that, and create the proper CASE for the file based on the browser.