-1

I have the following lines of code from .ashx file.

 public void ProcessRequest (HttpContext context)
  {
    string hstr = @"Data Source=SUMAN-PC\SQLEXPRESS;Initial Catalog=school;Integrated  Security=True";
    SqlConnection con = new SqlConnection(hstr);
    string ms = context.Request.QueryString["id_image"].ToString();
    con.Open();
    SqlCommand cmd = new SqlCommand("select img from class where classid=" + ms, con);
    SqlDataReader dr = cmd.ExecuteReader();
    dr.Read();
    context.Response.BinaryWrite((Byte[])dr[0]);
    context.Response.End();
}

After some study i found this

"The BinaryWrite method sends specific data to the current HTTP output without any character conversions."

What does it mean? How does it send data to http output? Where is HTTP output?

user35
  • 468
  • 1
  • 9
  • 24

1 Answers1

2

In this case context.Response is is a stream that represents the data that is being sent to the client (most likely the web browser) that issues the http request. Context.Response is being provided to you automatically by the ASP "engine" behind the scenes. Anything you write to that stream will end up being sent out to the browser.

The generic http handler automatically provides you with an HttpContext instance named Context. HttpContext contains two properties called Request and Response that allow you to access the request that your generic handler is serving and the corresponding response. The handler should do any processing that it needs to do and send the result by writing it's reply to the Response stream.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
  • OK This is helpful.SO BinaryWrite sends data to the browser?Can you provide the link for me to have detail study because i dont seem to find the good one. – user35 Jul 07 '14 at 04:52
  • Well, in this particular case BinaryWrite sends data to the browser, but BinaryWrite as actually a proxy method that belongs to the generic `Stream` class that the `Response` instance manages for you. Streams can actually point to anything, a file on the hard drive, a block of memory, another computer on a network, or even another stream. Think of streams like garden hoses. Water (data) goes in one end and comes out someplace else. In the case of `Context.Response` the ASP engine set everything up so that the `Response` stream can send data to whatever http client made the initial request. – Bradley Uffner Jul 07 '14 at 05:01
  • This MSDN article should give you a better idea how streams work in general. http://msdn.microsoft.com/en-us/library/system.io.stream(v=vs.110).aspx. Raw streams don't really provide good direct access to write data, so typically some form of `writer` object is used that can date data in whatever form you have and translate it in to the stream needs. In this case a `StreamWriter` is created that it hooked up to a stream that is hooked up to the browser. HttpResponse.BinaryWrite takes an array of bytes, sends that to a stream writer, which pushes those bytes in to an HttpStream. – Bradley Uffner Jul 07 '14 at 05:02
  • I am still not clear.You said the BinaryWrite() sends data to the browser.But where is this data received and how is this data used by the browser? – user35 Jul 07 '14 at 05:11
  • The browser can do whatever it wants with the data you send it typically it's used to display a web page. In that case the data you send is the raw html markup for the page to display. More recently ajax has allowed existing pages to request data directly. In the case of ajax the data can be used for virtually anything. It's all determined by what the JavaScript that made the request wants to do with it. – Bradley Uffner Jul 07 '14 at 05:16
  • It's misleading to say "the browser" because there are plenty of tools that talk HTTP without being browsers. – ta.speot.is Jul 07 '14 at 05:19
  • Looking at your code more closely i see that it seems to be pulling an image out of a data base and serving the raw bytes of the image to the browser. The browser would just be using these bytes to display the image as if it were a static image. – Bradley Uffner Jul 07 '14 at 05:21
  • Yes, "client" is a more exact words in this case, but i was trying to keep things fairly simple. – Bradley Uffner Jul 07 '14 at 05:22
  • 1 more thing can you tell me what is this (Byte[])dr[0] doing inside BinaryWrite.I mean,I know it is the data sent to the browser or client. Is it it converting the data from the datareader(dr) to the Byte[] format or is it something else? – user35 Jul 07 '14 at 05:43
  • That is the image data from the database. It's in the form of an array of bytes. Dr[0] means the first field on the reader. – Bradley Uffner Jul 07 '14 at 10:03