微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – Android手机在ASP.Net网站回发后没有打开PDF流

我有一个ASP.Net站点,在回发​​到初始页面后回流PDF文档.在IE,Chrome,Firefox以及iPhone和iPad上,一切正常,PDF被发送到浏览器.在 Android手机上,我收到一条错误消息,指出PDF无效.下面的代码是我尝试做的简化版本,但它确实重现了错误.我基本上在网页上有一个设置为在服务器上运行的按钮.在页面的第一个请求中,它显示HTML,在按钮单击后,它应该呈现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浏览器的某种缓存问题.
谢谢.

解决方法

尝试将当前请求转移到您将PDF直接呈现给响应流的其他处理程序.

您的网页代码

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

在新的RenderPDF.ashx中,移动到负责渲染pdf文件代码.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐