安卓浏览器无法下载PDF文件。

6
我有一个 Web 应用程序,让用户下载 PDF 文件。这是 ASP.NET MVC 架构的,桌面浏览器、iPhone、Android Chrome 和覆盖了 Web View setDownloadListener() 的本地 Android 应用都可以正常使用。

不过,在使用默认的 Android 浏览器时似乎无法下载。我在一台运行 4.2.2 的 Galaxy Nexus 和一台运行 2.3.7 的 Nexus S 4g 上进行测试。两个设备均已安装 PDF 阅读器应用程序。

当我在 Android Chrome 中测试时,用户会看到“开始下载...”的提示,然后状态栏中会显示“下载完成”的通知。用户可以点击该通知以在 PDF 阅读器中打开文件。但在 Android 的默认浏览器中,没有提示,也没有下载任何文件。

我使用 Wireshark 对网络进行抓包,发现如下请求:

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)

而响应是:

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)

从跟踪应用程序/ PDF内容中可以看出,内容已下载。您还可以看到我没有使用content-disposition标头,响应以gzip编码下载。

有任何想法,为什么默认的Android浏览器无法正常工作?用户无法访问下载的数据,也没有启动任何查看器(或意图)。响应被默默地忽略。(我猜测Android默认浏览器需要content-disposition,或者不喜欢下载gzip,但这只是我的猜测)

更新----

我删除了实现Gzip压缩的操作过滤器,并添加了Content-Disposition attachment标头。因此,控制器中的MVC代码如下:

public virtual ActionResult ViewPDF(string id)
{
    try
    {
        byte[] pdf = GetPDFContent(id);
        FileContentResult fcr = new FileContentResult(pdf, "application/pdf");
        fcr.FileDownloadName = id + ".pdf";            
        return fcr;
    }

现在的Http响应如下:

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)

现在已经没有gzip了,但是有一个content-disposition,但在Android默认浏览器中仍然看不到下载。

更新2 ---

尝试了https://dev59.com/P2445IYBdhLWcg3w1tkS#5728859http://winzter143.blogspot.com/2012/03/android-handling-of-content-disposition.htmlhttp://androidforums.com/application-development/256987-android-phone-download-issue-asp-net-website.html的建议,但结果仍然相同。


我有同样的问题。原生安卓浏览器和海豚浏览器无法下载。在其他浏览器中没有问题。我尝试了你提供的所有建议。 - Igby Largeman
找到了答案。希望能对你有所帮助。我在一个 iFrame 中下载,但 Android 浏览器好像不太喜欢。将 target="_blank" 添加到 <a> 中,现在它可以工作了。 - Michael Levy
不幸的是,这对我不适用 - 我通过表单提交到控制器操作来启动下载,该操作返回FileStreamResult。 - Igby Largeman
2
事实证明,我的问题是由于Android浏览器和Dolphin中的一个错误导致的,这会导致POST操作下载失败。我将我的操作更改为GET作为解决方法。https://code.google.com/p/android/issues/detail?id=1780 - Igby Largeman
1个回答

4

我找到了自己的答案,比我一直寻找的方法简单得多。我的下载链接页面在iframe中,Android默认浏览器中发生在iframe中的下载会丢失。我在a标签上添加了target="_blank"属性来启动下载,现在一切正常。

以下是演示问题和解决方法的示例代码:

<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>

以及 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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接