2011年5月21日土曜日

Android and the HTTP download file headers

I lately had to create a complex download repository for a customer.
Multiple files could be selected and were compressed on the fly into a
single ZIP file before being transfered to the client. After everything
worked fine with IE, FF and Chrome, I checked with Android 2.1 and the
download failed. Here is why, and how to cure it…

I faced a whole bunch of problems like the stock browser not reacting at
all, both browsers reporting that "The content being downloaded is not
supported by the phone", both browsers downloading the file but ignoring my
filename and trying to save the file under the script name that generated
the ZIP. I guess not many users would know how to handle a file that is
downloaded with the name "index.php" or similar. *sigh*

The cause

I finally found that the Android browsers are extremely picky about two of
the usual HTTP download headers. Here are some defunct header lines – just
to show how NOT to do it.

ATTENTION:
If you use any of the lines below your download will probably NOT WORK on
Android.

Content-Type: application/force-download
Content-Disposition: attachment; filename=MyFileName.ZIP
Content-Disposition: attachment; filename="MyFileName.zip"
And the solution

To make it work in Android's stock browser and in Dolphin browser you have
to use:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename="MyFileName.ZIP"
Please mind the double-quotes around the filename parameter and the
uppercase .ZIP extension of the filename. Both are absolutely necessary for
ZIP files on Android 2.1 to download correctly! From my web research it
looks like it is possible that the uppercase extension is only required for
ZIP files and only for Android versions up to 2.1. I am not sure of that and
did not do any further testing. If you create other downloadable file types
(e.g. PDF), please test yourself and leave a comment below.

Fortunately the above headers work for the major desktop browsers too, so
they are a good general solution.

Let's just hope that future Android versions will be less pesky.

Help to make it better

One important hint about a part of the problem (quoting the filename) came
from this Android bug report:

http://code.google.com/p/android/issues/detail?id=11422

If you have also been struggling with the problems mentioned in this post
and if you found a solution here, I humbly ask that you fuel the above bug
report by clicking the vote star at the bottom of the reporting page (above
the comment field).

0 件のコメント:

コメントを投稿