1

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 ?

8
  • Did you try it? What was the result? Commented Jun 4, 2018 at 16:55
  • @mason their was no change in terms of the behavior. I could not tell the performance part. Commented Jun 4, 2018 at 18:04
  • If there was no performance change, then why would you change it in the name of optimizing user performance? Commented Jun 4, 2018 at 18:05
  • @mason Because he's an inexperienced dev here for guidance, encouragement, and advice. Commented Jun 4, 2018 at 18:07
  • @TheSoftwareJedi I didn't ask you. Have you heard of the Socratic method of teaching? The point is to ask questions that get them to critically think about their issue, rather than just asking someone to give them the answer. Often works better than just telling someone the answer. Commented Jun 4, 2018 at 18:10

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

what are the other alternatives ?
@ramdev Alternatives to what?
Alternative to using OutputStream.Write
@ramdev Why? Why do you need an alternative to that?
@mason BinaryWrite or OutputStream.Write does solve my problem.I would like to know If there is a better to write binary data to Respons. If there is any . Appreciate your time.
0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.