• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps android phone download issue from asp.net website

pchalla

Lurker
Jan 13, 2011
1
0
hi guys,

any help is gr8tly appreciated (this is very urgent )

There seems to be an issue with streaming file downloads on the android phone. If I create a share for a file called photo.zip and try to download it from the "download.aspx" page I only get 13kb of the file then the download stops and says
 
I think your problem is your content-dispostion statment

context.Response.AddHeader("Content-Disposition", "attachment; filename=" & FileName)


thats what I use and it works for me (expect on android 2.1 which returns the wrong name)

I also let asp firgure out the content size

and I flush between the headers and the content then after the file

It took a lot of trial and error to get it to work but thats what ended up actually working
 
Upvote 0
I ran into this same issue as well.

Doing some research on the Android browser, the browser does not determine the file type from the file extension, like most desktop browsers. The Android browser determines the filetype through the ContentType property on the Response. To circumvent this, I just created a static dictionary initialized in Global.asax that sets mime types by file extension, like so....

public static void SetUpDictionary()
{
_FileExtentionToMimeTypeDictionary = new Dictionary<string, string>(33);
_FileExtentionToMimeTypeDictionary.Add("doc", "application/msword");
_FileExtentionToMimeTypeDictionary.Add("docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
_FileExtentionToMimeTypeDictionary.Add("jpeg", "image/jpeg");
_FileExtentionToMimeTypeDictionary.Add("jpg", "image/jpeg");
_FileExtentionToMimeTypeDictionary.Add("mpeg", "video/mpeg");
_FileExtentionToMimeTypeDictionary.Add("mpg", "video/mpeg");
_FileExtentionToMimeTypeDictionary.Add("pdf", "application/pdf");
_FileExtentionToMimeTypeDictionary.Add("zip", "application/zip");
_FileExtentionToMimeTypeDictionary.Add("xls", "application/excel");
_FileExtentionToMimeTypeDictionary.Add("xlw", "application/excel");
_FileExtentionToMimeTypeDictionary.Add("xlm", "application/excel");
_FileExtentionToMimeTypeDictionary.Add("xll", "application/excel");
_FileExtentionToMimeTypeDictionary.Add("xlt", "application/excel");
_FileExtentionToMimeTypeDictionary.Add("xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
_FileExtentionToMimeTypeDictionary.Add("xlsb", "application/vnd.ms-excel.sheet.binary.macroEnabled.12");
_FileExtentionToMimeTypeDictionary.Add("tiff", "image/tiff");
_FileExtentionToMimeTypeDictionary.Add("tif", "image/tiff");
_FileExtentionToMimeTypeDictionary.Add("gif", "image/gif");
_FileExtentionToMimeTypeDictionary.Add("flv", "video/x-flv");
_FileExtentionToMimeTypeDictionary.Add("pot", "application/powerpoint");
_FileExtentionToMimeTypeDictionary.Add("ppt", "application/powerpoint");
_FileExtentionToMimeTypeDictionary.Add("pptx", "application/vnd.openxmlformats-officedocument.presentationml.presentation");
_FileExtentionToMimeTypeDictionary.Add("bmp", "image/bmp");
_FileExtentionToMimeTypeDictionary.Add("html", "text/html");
_FileExtentionToMimeTypeDictionary.Add("htm", "text/html");
_FileExtentionToMimeTypeDictionary.Add("htmls", "text/html");
_FileExtentionToMimeTypeDictionary.Add("rt", "text/richtext");
_FileExtentionToMimeTypeDictionary.Add("rtf", "text/richtext");
_FileExtentionToMimeTypeDictionary.Add("rtx", "text/richtext");
_FileExtentionToMimeTypeDictionary.Add("txt", "text/plain");
_FileExtentionToMimeTypeDictionary.Add("text", "text/plain");
_FileExtentionToMimeTypeDictionary.Add("xml", "text/xml");
}

Our company took those as the most uploaded file types, but there are hundreds more. By changing your ContentType, hopefully you'll get some different results.
 
Upvote 0
@DavidAndroidDev:
You do not need to create this list of ext-to-mime relations if you already send the mime-type with a HTTP ContentType header! According to my tests it is also not true that Android does not look at the filename extension. You might end up with "The content being downloaded is not supported by the phone" if your extension is unknown or lowercased.

@all:
To make sure your downloads work as expected on all Android versions, make sure you...
1. set the ContentType to application/octet-stream
2. put the filename value in double quotes
3. put the filename extension in UPPERCASE

Read my blog for more details:
Android and the HTTP download file headers | DigiBlog

Cheers, Jpsy.
 
Upvote 0
@DavidAndroidDev:
You do not need to create this list of ext-to-mime relations if you already send the mime-type with a HTTP ContentType header! According to my tests it is also not true that Android does not look at the filename extension. You might end up with "The content being downloaded is not supported by the phone" if your extension is unknown or lowercased.

@all:
To make sure your downloads work as expected on all Android versions, make sure you...
1. set the ContentType to application/octet-stream
2. put the filename value in double quotes
3. put the filename extension in UPPERCASE

Read my blog for more details:
Android and the HTTP download file headers | DigiBlog

Cheers, Jpsy.

Hi there. i have the same problem with you. I cannot download file from android from my asp page. The different between my page is i'm using asp and you are using asp.net.

I have try instruction that you give us in the above quote. But, the problem is still the same. I cannot download file from my page while browsing using Samsung galaxy tab (android ver 3.1). But, i can download from my page using IE ver 7.

Can you help me to solve this problem? :thinking:
 
Upvote 0
For those people that are finding that an html page is appearing in their downloaded file, I suspect this is due to a 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
- Android download manager, I believe, then sends another HttpRequest GET to server for Download.aspx. I suspect that the download manager interprets the "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 constructed 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.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones