Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Image using ashx Handler

Tags:

c#

ashx

I have the following image in my aspx page

<td>
 <asp:Image ID="LargeImage" runat="server" Height="100" Width="100" />" 

</td>

In my aspx.cs, assigned a imageurl to this image

protected void uploadimage_Click(object sender, System.EventArgs e)
        {

            ImageUtils.uploadImage(Titletxt.Text, FileUpload.FileContent);
            LargeImage.ImageUrl = "~/AvatarImageFetch.ashx?memberid=" + memberid.ToString();
}

For some reason, the image doesn't show up. Here's my ashx

    public void ProcessRequest(HttpContext context)
        {
            SqlConnection myConnection = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["FMMImages"].ConnectionString);

            myConnection.Open();
            string sql = "select largeimage from images_temp where id=@memberid";
            SqlCommand cmd = new SqlCommand(sql, myConnection);
            int param;
            int.TryParse(context.Request.QueryString["memberid"], out param);
            cmd.Parameters.Add("@memberid", SqlDbType.Int).Value = param;
            //cmd.Parameters.Add("@GuID", SqlDbType.UniqueIdentifier).Value = context.Request.QueryString["UID"].ToString();

            cmd.CommandType = System.Data.CommandType.Text;

            SqlDataReader dReader = cmd.ExecuteReader();
            dReader.Read();
            context.Response.BinaryWrite((byte[])dReader["largeimage"]);
            dReader.Close();
            myConnection.Close();


        }

Also, I have a breakpoint in the ashx handler. Looks like the handler isn't firing.

like image 325
user575219 Avatar asked Jan 04 '12 20:01

user575219


People also ask

What is ASHX handler?

An ASHX file is a webpage that is used by the ASP.NET HTTP Handler to serve user with the pages that are referenced inside this file. The ASP.NET HTTP Handler processes the incoming request, references the pages from the . ashx file, and sends back the compiled page back to the user's browser.

How do you run ASHX?

An ashx file is a just a generic HTTP handler, so the easiest way to get this working is to create a new Web Site in the File menu, and just add the Handler. ashx file to the website root directory. Then, just run the site (F5) and browse to " YourSite/Handler. ashx ".

Can Adobe Open ASHX files?

ashx file extension. In this case, you can rename the file extension to . pdf and view it with Adobe Reader or another PDF viewer.


2 Answers

Try setting the ContentType:

context.Response.ContentType = "image/png";

http://www.dotnetperls.com/ashx

like image 161
RQDQ Avatar answered Oct 19 '22 07:10

RQDQ


Try the following in your ProcessRequest method:

context.Response.ContentType = "image";

using (System.IO.MemoryStream str = new System.IO.MemoryStream(objData.ToArray(), true))
{
       str.Write(objData.ToArray(), 0, objData.ToArray().Length);
       Byte[] bytes = str.ToArray();
       context.Response.BinaryWrite(bytes);
}

where objData is the value you are reading from the database

like image 26
TheBoyan Avatar answered Oct 19 '22 08:10

TheBoyan