且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何显示在数据库中的图像在Asp.net的图像控制?

更新时间:2021-11-21 02:44:34

您可以创建一个HttpHandler(ASHX)页面这将需要一个查询字符串,并设置为图像控件的ImageUrl属性

You can create an HttpHandler(ashx) page which would take a querystring and set that as the imageUrl property of the image control

<asp:image id="imgEmployee" imageUrl="DisplayImage.ashx?employeeId=<someId>"/>

现在在DisplayImage.ashx,您可以覆盖的ProcessRequest如下图所示: -

Now in DisplayImage.ashx, you can override the Proces-s-request like below:-

    public void ProcessRequest (HttpContext context) 
    { 
          int employeeId;
          if (context.Request.QueryString["employeeId"] != null)
   employeeId = Convert.ToInt32(context.Request.QueryString["employeeId"]);
          else
            throw new ArgumentException("No parameter specified");

        byte[] imageData= ;// get the image data from the database using the employeeId Querystring
        Response.ContentType = "image/jpeg"; // You can retrieve this also from the database
        Response.BinaryWrite(imageData);

    } 

Web.config中的变化: -

Web.config changes:-

<httpHandlers>
  <add verb="*" path="img/*" type="DisplayImage"/>
</httpHandlers>

详细这里和的这里

希望这有助于..