【问题标题】:Image is saved to the database as binary data, now I need to extract the data and put it in an <img> tag图像作为二进制数据保存到数据库中,现在我需要提取数据并将其放入 <img> 标记中
【发布时间】:2013-07-13 09:33:43
【问题描述】:

我需要帮助才能让我的图像在文字控件中创建的标签中正确显示。

        foreach (DataRow dr in dt.Rows)
        {
            string productName = dr["PRO_Name"].ToString();
            string productPrice = dr["PRO_Price"].ToString();
            byte[] productImg = (byte[])dr["PRO_Img"];

            string sProduct = @"<div class='four-shop columns isotope-item'>
                                 <div class='shop-item'>
                                  <figure>
                                  //I need the image from the data base here:
                                  <img src='" + productImg + "' alt='' />
                                  <figcaption center code herelass='item-description'>
                                  <h5>" + productName + "</h5>
                                  <span>R" + productPrice + "</span>
                                  <a href='#' class='button color'>Add to    Cart</a</figcaption>
                                  </figure>
                                  </div>
                                  </div>";
           //Im using a literal control to create the DIV dynamically for each product in the database.
           productPanel.Controls.Add(new LiteralControl(sProduct));
        }

【问题讨论】:

    标签: c# asp.net sql image


    【解决方案1】:

    您可以使用通用处理程序(.ashx) 获取产品名称并在ashx 文件中使用Context.WriteBinary() 方法。您应该将productName 作为查询字符串传递给ashx

    <img alt="Product image" src="~/somewhere/ImageHandler.ashx?productName=" + productName + "/>"
    

    【讨论】:

    • 谢谢,您的解决方案对我有用。我显然需要做一些额外的编码,但我明白了。
    【解决方案2】:

    根据您的浏览器需求和图像大小(我不建议将其用于大文件),您可以将其嵌入为 base64 字符串。看:Embedding base64 images

    【讨论】:

      【解决方案3】:

      您应该将图像的 URL 放在 imgsrc 属性中,而不是像现在这样放在 byte 数组中:

      //Wrong:
      byte[] productImg = (byte[])dr["PRO_Img"];
      <img src='" + productImg + "' alt='' />
      

      为了实现它,看看这个关于如何创建 ASP.NET ASHX 处理程序并最终使用 URL 检索图像的答案:

      Display image from database in ASP.net with C#

      【讨论】:

        【解决方案4】:

        试试这个

        查看:

        <img src="@Url.Action("GetImg", "Home", new {id= Model.id})" alt="" width="150" height="200"/>
        

        控制器:

            public FileContentResult GetImg(int id)
            {
                byte[] byteArray = new Model().GetImgByID(id);
        
                if (byteArray != null)
                {
                    return new FileContentResult(byteArray, "image/png");
                }
                else
                {
                    //something failed, image not found!
                    return null;
                }
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-11-12
          • 1970-01-01
          • 1970-01-01
          • 2017-02-04
          • 1970-01-01
          • 2018-03-29
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多