Search code examples
c#asp.nethttpgeneric-handler

How does the binarywrite() method works?


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?


Solution

  • 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.