Android浏览器不会下载PDF文件浏览器、文件、Android、PDF

2023-09-05 02:18:16 作者:哼着情歌、享受寂寞

我有一个Web应用程序,让用户下载一个PDF文件。这是ASP.NET MVC,这一切工作正常的桌面浏览器,iPhone,Android的浏览器,并在原生Android应用程序,覆盖了Web视图setDownloadListener()。

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().

然而,在下载似乎不使用默认的Andr​​oid浏览器的时候工作。我测试在运行4.2.2的Galaxy Nexus和上一台Nexus的4G运行2.3.7。我安装了两台设备上的PDF阅读器应用程序。

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.

当我在测试Android的浏览器,用户将看到一个开始下载...敬酒,然后下载完整的通知显示在状态栏。用户可以触摸这个通知在PDF查看器打开该文件。在Android的默认浏览器,没有面包,没有下载文件被保存。

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.

我把Wireshark的在网络上,我看到了以下要求:

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)

和反应是:

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内容请参阅下载。你也可以看到,我不使用的内容处置头和响应下载使用gzip编码。

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.

为什么这不会在默认的Andr​​oid浏览器工作的任何想法?的用户不具有访问所下载的数据和没有观看者(或意图)被启动。响应只是忽略。 (我猜的Andr​​oid默认浏览器需要的内容,处置或不喜欢Gzip已上下载,但我只是猜测)

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)

更新----

我删除了那个正在实施Gzip已融为一体pression行为过滤和我说的 内容处置附件头。因此,MVC code在我的控制器是这样的:

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

和HTTP响应现在是:

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)

不再有任何gzip和有一个内容处置,但在Android默认浏览器目前还没有看到下载。

There is no longer any gzip and there is a content-disposition, but on the Android default browser there is still no visible download.

更新2 ---

试着从 http://stackoverflow.com/a/5728859/90236 和的http://winzter143.blogspot.com/2012/03/android-handling-of-content-disposition.html和http://androidforums.com/application-development/256987-android-phone-download-issue-asp-net-website.html但我仍然得到同样的结果。

tried the suggestions from http://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.

推荐答案

找到我自己的答案,它是如此不到哪里我一直在寻找更简单。我的下载链接页面中的iframe。东西在Android默认浏览器失去您的下载,如果它发生在一个IFRAME。我添加的目标=_空白,以发起下载和现在的一切工作正常的标签。

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.

下面是一些示例code来演示问题和修复。

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>

和inner.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>