Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
663 views
in Technique[技术] by (71.8m points)

c# - Display Image using ashx Handler

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try setting the ContentType:

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

http://www.dotnetperls.com/ashx


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...