ram dev
ram dev

Reputation: 119

Response.outputstream.write vs Response.BinaryWrite

We use the below code to show PDF files using Response.BinaryWrite. We are exploring new options as to optimize the user performance.

        Response.Clear();
        Response.ContentType = "application/pdf";

        if (Page.Request!=null && Page.Request.Browser!=null && 
            (!(Page.Request.Browser.Type.Contains("IE") || Page.Request.Browser.Type.Contains("InternetExplorer"))))
        {
            Response.AddHeader("Content-Length", buffer.Length.ToString());
        }

        Response.AddHeader("Content-Encoding", "deflate");
        if (Request.Browser.Browser == "IE" && Request.Browser.MajorVersion < 7)
            Response.AddHeader("Content-Disposition", "attachment; filename=document.pdf");            
        Response.OutputStream.Write(buffer, 0, buffer.Length);//Response.BinaryWrite(buffer);
        Response.End();
        Response.Flush();
        Response.Close();

I read that Response.outputstream.write is another option to render the PDF. Using Response.outputstream.write will have any added advantage ?

Upvotes: 1

Views: 3403

Answers (2)

sglugove
sglugove

Reputation: 71

If you're experiencing rendering delays, you could implement an async method and use the following code:

var result = Response.OutputStream.BeginWrite(buffer, 0, BufferedStream.Length, null, null);
Response.OutputStream.EndWrite(result);

Upvotes: 0

TheSoftwareJedi
TheSoftwareJedi

Reputation: 35236

No, it will not have any advantage. The code for BinaryWrite simply calls Write on the OutputStream as you are already doing:

public void BinaryWrite(byte[] buffer)
{
  this.OutputStream.Write(buffer, 0, buffer.Length);
}

Using a tool such as DotPeek you can step right into compiled code and look at what it is doing. That's what I did in this case. DotPeek is free and available from JetBrains.

This can help you research these things yourself instead of having to guess, test, or ask.

Upvotes: 1

Related Questions