开发者

Android Phones not opening PDF streamed after postback in ASP.Net Site

I have an ASP.Net site that streams back a PDF document after a postback to the initial page. In IE, Chrome, Firefox, and on iPhones and iPads, everything works fine, the PDF is sent to the browser. On Android phones, I get an error stating that the PDF is invalid. The code below is a simplified version of what I am trying to do, but it does reproduce the error. I basically have a button on a web page that is set to run at server. On the first request of the page it displays the HTML and after the button click, it should render the PDF:

protecte开发者_开发知识库d 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();

        }
    }


}

I have played a lot with different values in the headers and different ways of closing and flushing the response stream with no luck. Has anyone seen this before or can anyone offer any suggestions on things to try.

Edit: I have found out that this only happens when I do a postback to the page. If I just stream the document right off the bat, the document streams correctly. This leads me to believe it is some sort of caching problem with the android browser. Thanks.


Try transfer current request to other handler where you render PDF directly to response stream.

Your page code:

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

And in that new RenderPDF.ashx, move here that code responsible for rendering pdf file.


Android does not seem to like Content-Disposition much, from what I can tell. I recommend writing the PDF out to a file and issuing a redirect to the URL pointing to that file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜