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 An开发者_如何学Cdroid 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...
- set the ContentType to application/octet-stream
- put the Content-Disposition filename value in double quotes
- 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.
As I wrote at downloading files from android:
Android browser will not download file in button Post events. In post events the file will be some .htm garbage file. to over come this do as below.
In download button click
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
Response.Redirect("download-file.aspx");
}
and on download-file.aspx file do as below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class mobile_download_file : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filename = "usermanual.pdf";
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + "" + filename + "");
Response.Write(Server.MapPath(Request.ApplicationPath) + "\\" + filename);
Response.TransmitFile(Server.MapPath(Request.ApplicationPath) + "\\" + filename);
Response.End();
}
}
the same can be implemented in php also.
I have tried all recommendations from Jspy blog and nothing worked so far. Content-disposition brings browser in downloading mode, however nothing gets downloaded except HTML of page from which download got initiated. So my conclusion, it is pure bug from Google and we may only pray for that Google fix it. My work around was to set content type to some type coming from Accept header form mobile browser. It generally works, you can even download zip files as text.
In theory, the filename parameter should be set on Content-Disposition, not Content-Type. Not sure whether this will help with the Android browser.
精彩评论