Avoiding content type issues when downloading a file via browser on Android

If I have a file made available to a browser through my webapp, I normally just set the URL to something like http://website.com/webapp/download/89347/image.jpg. I then set the HTTP headers Content-Type: application/octet-stream; filename=image.jpg and Content-Disposition: Attachment.

However, on the Android. It seems the only way I can get the file to download is to set Content-Type: image/jpg. Otherwise the file name says <Unknown> and an error comes

Download unsuccessful
Cannot download. The content is not supported on this phone

Is there any way I can get Android to download and open the file through the browser without keeping a list of mime types?


To make any downloads work on all (and especially older) Android versions as expected, you need to...

  1. set the ContentType to application/octet-stream
  2. put the Content-Disposition filename value in double quotes
  3. write the Content-Disposition filename extension in UPPERCASE

Read my blog post for more details:
http://digiblog.de/2011/04/19/android-and-the-download-file-headers/


Dmitriy (or others looking for a possible solution) if an html page is appearing in your downloaded file, I suspect this is due to the double HttpRequest GET issue. A typical scenario is the following POST, Redirect, GET model:

  • Android browser issues a HttpRequest POST to server (e.g. submit button or link to request a download file, filename.ext say)

  • Server streams the requested filename.ext to bytes, stores in a session variable, and then issues a Response.Redirect to Download.aspx, for example, to handle the response object construction

  • Android browser correctly sends HttpRequest GET to server for Download.aspx

  • Server responds with typical Content-Disposition: attachment; filename="filename.ext" style construct with the response object containing the requested filename.ext, being the bytes in the session variable.

  • Android download manager, I believe, then sends another HttpRequest GET to server for Download.aspx. I suspect that the download manager interprets the previous "attachment" response as a trigger to send this second GET.

  • Server (Download.aspx) again tries to construct the response object to send back to the browser.

  • Android download manager downloads filename.ext, using the response object contents from the second Download.aspx.

In many scenarios this would be fine. But if, for example, the server in the Download.aspx code does some housekeeping and removes the session variable the first time it is called, then the next time around there is no session variable. So, depending on how the code is written it is possible that the response object doesn't get explicity constructed and maybe the Response.End doesn't get called and so only the Download.aspx's html ends up being sent.

This is what we discovered using Wireshark, although I admit I am assuming it is the Android download manager that is the cause for the double GET.

I hope this explanation has been of some help.