user740240
user740240

Reputation: 61

Downloading files in an Android 3.0 browser from my ASP.NET site

I'm having an issue downloading a PDF file from my custom asp.net site in the out of the box browser on an Android tablet (a Motorola Xoom).

I'm using Response.OutputStream.Write to send the file because it's having to read the file from a UNC path. This method works on IE, Firefox, Safari, and on iPad, but doesn't work on the browser on the Xoom. However, when I downloaded Firefox on the Xoom I was able to download the file just fine.

I've found a couple of spots suggesting I use the following headers: Content-Type: application/octet-stream Content-Disposition: attachment; filename="MyFileName.PDF"

I've tried this with no luck. I also tried using Content-Types of application/pdf and application/force-download and every combination of filename capitalized, extension capitalized, extension lowercase, double quotes, single quotes, no quotes, etc... for Content-Disposition and have yet to find anything that works.

I have also found that if I execute the code to download the file on Page_Load it is able to download, but if I do a postback and then execute the code (clicking on a link button to download the file) it doesn't work

Has anyone been able to download a file using custom C# code on the OOTB Android 3.0 browser?

Upvotes: 6

Views: 4281

Answers (2)

Ethan Drotning
Ethan Drotning

Reputation: 11

This is old but in case anyone comes across this issue:

Check unknown sources under settings --> applications.

Make sure you have PDF defined in MIME Types (application/pdf) on the IIS server.

Also if you are using a file handler you will need to set the header info.

This code block is an example of what would be used in a file handler:

string dirpath = System.Configuration.ConfigurationManager.AppSettings["FilePath"];
string file = System.Configuration.ConfigurationManager.AppSettings["FileName"];

System.IO.FileInfo fi = new System.IO.FileInfo(dirpath + file);
string contentlen = fi.Length.ToString();

context.Response.Buffer = true;
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment; filename=" + file);
context.Response.AddHeader("content-length", contentlen);
context.Response.ContentType = "application/pdf";

try
{
    context.Response.WriteFile(dirpath + file);
}
catch (Exception)
{
    context.Response.Redirect("Error.aspx");
}

Upvotes: 1

Michael
Michael

Reputation: 2940

I have no idea as to why this is happening, but if you can download a file inside the Page_Load block, the a workaround would be linking to a dedicated page for downloading where you call the download function inside the Page_Load block.

To make this more compliant with the rest of your website (if needed) you might pass different parameters to the page for downloading different files, or redirect to this page only if you detect the site is running on the android browser.

I will add code examples shortly.

Upvotes: 0

Related Questions