Android and the HTTP download file headers > HTML5 관련 정보

본문 바로가기

검색취소

Android and the HTTP download file headers

검색 이전 다음 목록

본문

The cause

I finally found that the Android browsers are extremely picky about two of the usual HTTP download headers. As I experienced this behavior with several browsers on a Motorola Defy running Android 2.1 (Eclair) but not after an update to 2.2 (Froyo), I conclude that this is an Android problem and not a browser issue.

Here are some header lines that did not work on Android 2.1 ? 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 2.1.

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.

How to create the headers  [UPDATE 20110623]

How you create those headers largely depends on your scenario and your programming language.
In PHP it would look like this:

header("Content-Type: application/octet-stream"); header("Content-Disposition: attachment; filename=\"$zipBaseName.ZIP\"");

If you are dealing with static downloadable files on an Apache webspace and you only want to link to them from some web page, you have to inform Apache to use the right headers, e.g. by appropriate statements in an .htaccess file somewhere up in the path of your downloadable file. If we would not have to include the filename in the Content-Disposition header this would be simple:

<Files *.ZIP> ForceType application/octet-stream Header set Content-disposition "attachment" </Files>

But to include the filename on the fly we have to do some more magic:

<Files *.ZIP> ForceType application/octet-stream SetEnvIf Request_URI "^.*/([^/]*)$" MYFILENAME=$1 Header set Content-disposition "attachment; filename=\"%{MYFILENAME}e\"" UnsetEnv MYFILENAME </Files>

And don’t forget to store your ZIP files with uppercase extensions, as Apache will not change your lyrics for you!

Fortunately the above headers will also work for all major desktop browsers, so they are a good general solution. And let’s just hope that future Android versions will be less pesky.

Testing Different Headers [UPDATE 20111025]

Here is a little script to quickly test the reaction of your Android gadget with different header combinations:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename=”digiblog.ZIP”
TRY DOWNLOAD (Should work on all versions of Android!)

Content-Type: application/octet-stream
Content-Disposition: attachment; filename=”digiblog.zip”
TRY DOWNLOAD (May NOT work on older versions of Android ? lowercase filename extension!)

Content-Type: application/octet-stream
Content-Disposition: attachment; filename=digiblog.ZIP
TRY DOWNLOAD (May NOT work on older versions of Android ? filename not quoted!)

Content-Type: application/force-download
Content-Disposition: attachment; filename=”digiblog.ZIP”
TRY DOWNLOAD (May NOT work on older versions of Android ? wrong Content-Type!)

To create your own tests you may use this URL syntax:
http://www.digilog.de/pub/blog/android_download_header_test.php/param1/param2/param3/…/
where paramX may be any of the following strings (headers are sent in order of parameters):

ctaos = Content-Type: application/octet-stream
ctafd =  Content-Type: application/force-download
cdafqXXX  (XXX may be GIF, PDF, ZIP, gif, pdf, zip)
= Content-Disposition: attachment; filename=”digiblog.XXX”
cdafXXX  (XXX may be GIF, PDF, ZIP, gif, pdf, zip)
= Content-Disposition: attachment; filename=digiblog.XXX

Examples:
the four preconfigured tests above use these links…
? http://www.digilog.de/pub/blog/android_download_header_test.php/ctaos/cdafqZIP/
? http://www.digilog.de/pub/blog/android_download_header_test.php/ctaos/cdafqzip/
? http://www.digilog.de/pub/blog/android_download_header_test.php/ctaos/cdafZIP/
? http://www.digilog.de/pub/blog/android_download_header_test.php/ctafd/cdafqZIP/

When you use this testing feature please leave a comment and tell us about your Android version and your results! Also, if you need additional headers for your testing, drop a comment and I will add them. 

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

등록된 댓글이 없습니다.

?>