2011-08-15 68 views
4

我有一个ASP.Net网站在回传到初始页面之后流回PDF文档。在IE,Chrome,Firefox以及iPhone和iPad上,一切正常,PDF发送到浏览器。在Android手机上,我收到错误消息,指出PDF无效。下面的代码是我试图做的简化版本,但它确实重现了错误。我基本上有一个设置在服务器上运行的网页上的按钮。在页面的第一个请求时,它会显示HTML和按钮点击之后,它应该呈现的PDF:Android手机未在ASP.Net网站发布回传后流式传输PDF网站

protected void Page_Load(object sender, EventArgs e) 
{ 

    Response.ClearHeaders(); 
    Response.AppendHeader("Cache-Control", "no-cache"); //HTTP 1.1 
    Response.AppendHeader("Cache-Control", "private"); // HTTP 1.1 
    Response.AppendHeader("Cache-Control", "no-store"); // HTTP 1.1 
    Response.AppendHeader("Cache-Control", "must-revalidate"); // HTTP 1.1 
    Response.AppendHeader("Cache-Control", "max-stale=0"); // HTTP 1.1 
    Response.AppendHeader("Cache-Control", "post-check=0"); // HTTP 1.1 
    Response.AppendHeader("Cache-Control", "pre-check=0"); // HTTP 1.1 
    Response.AppendHeader("Pragma", "no-cache"); // HTTP 1.1 
    Response.AppendHeader("Keep-Alive", "timeout=3, max=993"); // HTTP 1.1 
    Response.AppendHeader("Expires", "0"); // HTTP 1.1 

    if (IsPostBack) 
    { 
     Response.ClearContent(); 
     Response.ClearHeaders(); 

     string fullPath = @"C:\Temp\outdoc.pdf"; 

     if (System.IO.File.Exists(fullPath)) 
     { 

      //Set the appropriate ContentType. 
      Response.ContentType = "application/pdf"; 
      //Response.ContentType = "application/octet-stream"; 
      Response.AddHeader("Content-Disposition", "attachment;filename=\"TestDoc.pdf\""); 

      byte[] b = System.IO.File.ReadAllBytes(fullPath); 
      this.Page.Response.AddHeader("Content-Length", b.Length.ToString()); 

      //Write the file directly to the HTTP content output stream. 
      Response.BinaryWrite(b); 
      Response.Flush(); 
      Response.End(); 
      Response.Close(); 

     } 
    } 


} 

我已经打了很多在头不同的价值观和关闭不同的方式和冲洗响应流没有运气。有没有人看过这个或任何人都可以提供任何建议的事情来尝试。

编辑:我发现这只发生在我回发页面时。如果我只是将文档直接从文件中传出,文档就可以正确传输。这导致我相信这是Android浏览器的某种缓存问题。 谢谢。

回答

1

尝试传送当前请求到其他处理程序,您将PDF直接呈现给响应流。

网页代码:

if (IsPostBack) 
{ 
    Context.Server.Transfer("RenderPDF.ashx"); 
    Response.End(); 
} 

而在这新RenderPDF.ashx,搬到这里来负责渲染PDF文件的代码。

+0

谢谢。我会给这个镜头,让你知道。 –

0

Android似乎不喜欢Content-Disposition很多,从我可以告诉。我建议将PDF文件写入文件并发送重定向到指向该文件的URL。

+0

这对我来说并不是一个好的解决方案,因为在我真正的解决方案中,我是从数据库流式传输数据来创建PDF,而且我不希望在服务器上存在临时副本。然后,我遇到了很多清理文件和确保名称唯一性的问题。另外,如果我不做回发,这似乎不是问题,我已经发现并将添加到原始帖子。 –

+0

@John Koerner:“我不希望在服务器上安装临时文件” - 将其粘贴在特定的目录中,并使用“cron”作业每晚清理较旧的PDF文件。 “并确保名称的唯一性” - 使用GUID。 – CommonsWare

+0

问题是,我根据用户的输入解密了一些数据,并且我无法将解密的数据在服务器的文件系统上保存任何时间段。 –