简体   繁体   English

使用ashx Handler显示图像

[英]Display Image using ashx Handler

I have the following image in my aspx page 我的aspx页面中有以下图像

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

</td>

In my aspx.cs, assigned a imageurl to this image 在我的aspx.cs中,为此图像指定了一个图像

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 这是我的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. 另外,我在ashx处理程序中有一个断点。 Looks like the handler isn't firing. 看起来处理程序没有触发。

Try setting the ContentType: 尝试设置ContentType:

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

http://www.dotnetperls.com/ashx http://www.dotnetperls.com/ashx

Try the following in your ProcessRequest method: 在ProcessRequest方法中尝试以下操作:

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 其中objData是您从数据库中读取的值

The ImageUrl only replaces the tilde (~) in the control markup. ImageUrl仅替换控件标记中的波浪号(〜)。

Try this instead: 试试这个:

string imageUrl = "~/AvatarImageFetch.ashx?memberid=" + memberid.ToString();
LargeImage.ImageUrl = Page.ResolveUrl(imageUrl);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM