Android browser won't download pdf file
android, asp.net-mvc, download, pdf
Solution
Found my own answer and it was so much simpler than where I was looking. My page with the download link is in an iframe. Something in the Android default browser loses your download if it occurs in an iframe. I added target="_blank" to the a tag that initiates the download and everything now works fine.
Here is some sample code to demonstrate the the problem and the fix.
<html>
<head><title>Test page</title></head>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<body>
<a href="http://www.adobe.com/products/eulas/pdfs/Photoshop_On_a_Server_Policy_5-31-2011.pdf">
Download sample pdf</a><br />
<iframe src="inner.html" />
</body>
</html>
and inner.html:
<html>
<head><title>test iframe</title></head>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<body>
<p>This does NOT work on Android default browser.
<a href="http://www.adobe.com/products/eulas/pdfs/Photoshop_On_a_Server_Policy_5-31-2011.pdf">
Download sample pdf within iframe</a></p>
<p><a href="http://www.adobe.com/products/eulas/pdfs/Photoshop_On_a_Server_Policy_5-31-2011.pdf"
target="_blank">Download sample pdf within iframe with target</a>.</p>
</body>
</html>
Problem
I have a web app that lets users download a pdf file. It is ASP.NET MVC and it all works fine on desktop browsers, iPhone, Android Chrome, and in a native Android app that overrides the Web View setDownloadListener(). However, the download doesn't seem to work when using the default Android browser. I'm testing on a Galaxy Nexus running 4.2.2 and on a Nexus S 4g running 2.3.7. I have PDF viewer apps installed on both devices. When I test in Android Chrome, the user sees a "Starting Download..." toast and then a Download complete notification is shown in the status bar. The user can touch this notification to open the file in a pdf viewer. In Android's default browser there is no toast and no download file is saved. I put wireshark on the network and I see the following request: ``` GET /appapth/ViewPDF/8383600280757433_11_05_2012 HTTP/1.1 Host: 192.168.1.117 Connection: keep-alive Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 X-Requested-With: com.google.android.browser User-Agent: Mozilla/5.0 (Linux; U; Android 4.2.2; en-us; Galaxy Nexus Build/JDQ39) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30 Accept-Encoding: gzip,deflate Accept-Language: en-US Accept-Charset: utf-8, iso-8859-1, utf-16, *;q=0.7 Cookie: (lots o'cookie data) ``` and the response is: ``` HTTP/1.1 200 OK Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Content-Type: application/pdf Content-Encoding: gzip Expires: -1 Server: Microsoft-IIS/7.5 Set-Cookie: ___TempData=; path=/ Set-Cookie: ___TempData=(lot's o'cookie data); path=/; HttpOnly Date: Mon, 01 Apr 2013 18:22:20 GMT Content-Length: 48244 ......YQ.....T...?J.h)...!E.S(lots o'binary data) ``` As you can see from the trace application/pdf content is downloaded. You can also see that I'm not using the content-disposition header and the response is downloaded with gzip encoding. Any ideas on why this does not work on the Default Android browser? The user does not have access to the downloaded data and no viewer (or intent) is launched. The response is just silently ignored. (I'm guessing that Android default browser requires the content-disposition or doesn't like Gzip on the download, but I'm just guessing) Update ---- I removed our action filter that was implementing Gzip compression and I added in the Content-Disposition attachment header. so, MVC code in my controller is like: ``` public virtual ActionResult ViewPDF(string id) { try { byte[] pdf = GetPDFContent(id); FileContentResult fcr = new FileContentResult(pdf, "application/pdf"); fcr.FileDownloadName = id + ".pdf"; return fcr; } ``` and the Http response is now: ``` HTTP/1.1 200 OK Cache-Control: no-cache, no-store, must-revalidate Pragma: no-cache Content-Type: application/pdf Expires: -1 Server: Microsoft-IIS/7.5 Content-Disposition: attachment; filename=433_07_05_2012.pdf Set-Cookie: ___TempData=; path=/ Set-Cookie: ___TempData=(lots o'cookie data);path=/; HttpOnly Date: Mon, 01 Apr 2013 19:56:07 GMT Content-Length: 59069 %PDF-1.3 %.... 13 0 obj << (lots o'binary pdf data) ``` There is no longer any gzip and there is a content-disposition, but on the Android default browser there is still no visible download. Update 2 --- tried the suggestions from https://stackoverflow.com/a/5728859/90236 and http://winzter143.blogspot.com/2012/03/android-handling-of-content-disposition.html and http://androidforums.com/application-development/256987-android-phone-download-issue-asp-net-website.html but I still get the same results.