简体   繁体   English

图像以二进制数据的形式保存到数据库中,现在我需要提取数据并将其放入 <img> 标签

[英]Image is saved to the database as binary data, now I need to extract the data and put it in an <img> tag

I please need help to get my image to display correctly in the tag that is created in a literal control. 我需要帮助以使我的图像正确显示在文字控件中创建的标记中。

        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));
        }

You may use a generic handler (.ashx) that get productName and use Context.WriteBinary() method in ashx file. 您可以使用获取(.ashx)的通用处理程序(.ashx) ,并在ashx文件中使用Context.WriteBinary()方法。 You should pass productName to ashx as a query string: 你应该通过productNameashx作为查询字符串:

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

You should put the URL of your image in the img 's src attribute and not a byte array as you do now: 您应该将图像的URL放在imgsrc属性中,而不是像现在那样将byte数组放置:

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

In order to achieve it, take a look at this answer about how to create an ASP.NET ASHX Handler and eventually retrieve the image using a URL: 为了实现它,请看以下有关如何创建ASP.NET ASHX处理程序并最终使用URL检索图像的答案:

Display image from database in ASP.net with C# 使用C#从ASP.net中的数据库显示图像

Depending on your browser needs and image size (I don't recommend it for large files), you may embed it as a base64 string. 根据您的浏览器需求和图像大小(不建议将其用于大文件),您可以将其作为base64字符串嵌入。 Look at: Embedding base64 images 查看: 嵌入base64图像

Try this 尝试这个

View: 视图:

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

Controller: 控制器:

    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;
        }
    }

暂无
暂无

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

相关问题 如何将二进制数据转换为图像并显示在Div标签或img标签中? - how to convert binary data to image and display in Div Tag or img Tag? 如何将Sql Server数据库中的二进制数据转换回image.used内存流。 它已保存,现在尝试从Sql Server中检索 - How to convert binary data in Sql Server database back to image.used Memory Stream. It saved and now trying to retrieve from Sql Server 如何将图像数据传递给 img 标签? - How to pass image data to img tag? 我有一个将数据保存到数据库的 post 方法,我需要返回保存记录的 id 并将另一个方法作为 Foreigh Key 传递 - I have a post method that saved data to database, I need to return the id of the saved record and pass it another method as a Foreigh Key 显示从数据库中取出的二进制流作为html内的“图像” <img> 标签 - display a binary stream fetched from database as “image” inside the html as an <img> tag 我必须在何处以及如何保留每个文件的二进制数据,因为以后需要将它们保存在数据库中 - Where and how I have to keep binary data of each file, because I need them after for saving in a database 提取标签内的数据 - Extract data inside a tag 如何在数据列表控件中显示路径保存在数据库中的图像? - How to display an image whose path is saved in the database in a data list control? 我想把所有的数据库数据放在列表中 - I want to put all the database data in the list 我可以提取调试时可以检查的二进制数据吗? - Can I extract binary data that I can inspect while debugging?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM