Response.outputstream.write vs Response.BinaryWrite

时间:2018-06-04 16:54:11

标签: c# asp.net httpresponse outputstream

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 ?

2 个答案:

答案 0 :(得分:0)

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.

答案 1 :(得分:0)

如果遇到渲染延迟,则可以实现异步方法并使用以下代码:

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