Android浏览器不会下载pdf文件 [英] Android browser won't download pdf file

查看:245
本文介绍了Android浏览器不会下载pdf文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用程序,让用户下载一个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 
主机:192.168.1.117
连接:keep-alive
接受:text / html,application / xhtml + xml,application / xml; q = 0.9,* / *; q = 0.8
X-Requested-With:com.google.android.browser
用户代理:Mozilla / 5.0(Linux; U; Android 4.2.2; en-us; Galaxy Nexus Build / JDQ39)AppleWebKit / 534.30(KHTML,像Gecko)版本/ 4.0 Mobile Safari / 534.30
接受编码:gzip, deflate
接受语言:en-US
接受字符集:utf-8,iso-8859-1,utf-16,*; q = 0.7
Cookie:(很多o'cookie数据)

,响应是:

  HTTP / 1.1 200 OK 
缓存控制:无缓存,无存储,必须重新生效
Pragma:no-cache
内容类型:application / pdf
内容编码:gzip
过期:-1
服务器:Microsoft-IIS / 7.5
Set-Cookie:___ TempData =; path = /
Set-Cookie:___ TempData =(lot's o'cookie data);路径= /; HttpOnly
日期:2013年4月1日,星期一18:22:20 GMT
内容长度:48244

...... YQ ..... T .. 。?Jh)...!ES(很多二进制数据)

正如你可以看到的下载跟踪应用程序/ pdf内容。您还可以看到我没有使用content-disposition标题,并且使用gzip编码下载响应。



任何关于为什么这不工作在默认Android浏览器?用户无权访问下载的数据,并且不会启动查看器(或意图)。回应只是默默无视。 (我猜测Android的默认浏览器需要内容处理或不喜欢Gzip下载,但我只是猜测)



更新--- -



我删除了正在实现Gzip压缩的动作过滤器,并在
Content-Disposition附件标题中添加。所以我的控制器中的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 
缓存控制:无缓存,无存储,必须重新生效
Pragma:no-cache
内容类型:application / pdf
过期日期:-1
服务器:Microsoft-IIS / 7.5
Content-Disposition:attachment; filename = 433_07_05_2012.pdf
设置Cookie:___ TempData =; path = /
Set-Cookie:___ TempData =(lots o'cookie data); path = /; HttpOnly
日期:2013年4月1日,星期一19:56:07 GMT
内容长度:59069

%PDF-1.3%.... 13 0 obj<<<< ; (很多o'binary pdf数据)

不再有任何gzip,还有内容处理,但在Android默认浏览器上仍然没有可见的下载。



更新2 ---



https://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 ,但我仍然得到相同的结果。

解决方案

找到我自己的答案,它比我正在寻找的要简单得多。我的页面与下载链接在一个iframe。 Android默认浏览器中的某些东西如果发生在iframe中,则会丢失您的下载。我将target =_ blank添加到启动下载的标签中,现在所有内容都可以正常工作。



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

 < html> 
< head>< title>测试页< / title>< / head>
< META HTTP-EQUIV =CACHE-CONTROLCONTENT =NO-CACHE>
< body>
< a href =http://www.adobe.com/products/eulas/pdfs/Photoshop_On_a_Server_Policy_5-31-2011.pdf>
下载示例pdf< / a>< br />
< iframe src =inner.html/>
< / body>
< / html>

and inner.html:

 < HTML> 
< head>< title> test iframe< / title>< / head>
< META HTTP-EQUIV =CACHE-CONTROLCONTENT =NO-CACHE>
< body>
< p>这不适用于Android默认浏览器。
< a href =http://www.adobe.com/products/eulas/pdfs/Photoshop_On_a_Server_Policy_5-31-2011.pdf>
下载iframe中的示例pdf< / a>< / p>

< p>< a href =http://www.adobe.com/products/eulas/pdfs/Photoshop_On_a_Server_Policy_5-31-2011.pdf
target =_ blank >在iframe中使用目标< / a>下载样本pdf< / p>
< / body>
< / html>


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.

解决方案

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>

这篇关于Android浏览器不会下载pdf文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆