简体   繁体   English

我想将数据库中的图像调用到ASP:repeater控件中,但是我正在 <image src“System.Byte[]”> 结果

[英]I want to call images from database in to ASP:repeater control but I am getting <image src“System.Byte[]”> in result

This is my code in aspx file 这是我在aspx文件中的代码

cnn.Open();
    SqlDataAdapter da1 = new SqlDataAdapter("select * from carousel", cnn);
    DataTable dt1 = new DataTable();
    da1.Fill(dt1);
    Rp1.DataSource = dt1;
    Rp1.DataBind();
cnn.Close();

and this is repeater 这是中继器

<asp:Repeater id="Rp1" runat="server">
        <ItemTemplate>
        <div class="item">
            <asp:Image ID="Image1" ImageUrl='<%# Eval("image") %>' runat="server" />
        </div>
        </ItemTemplate>
        <footertemplate></footertemplate>
</asp:Repeater>

I tried everything but I am getting in result every time I really want some help on this, I am new to ASP.Net 我尝试了一切,但是每次我真的想要一些帮助时,我都会得到成果,我是ASP.Net的新手。

This is because the value coming from the database is a byte array representing the actual data of the image. 这是因为来自数据库的值是表示图像实际数据的字节数组。 Whereas the src of an img tag expects a URL to an image. img标签的src需要图像的URL。 There are essentially two ways to go about this... 基本上有两种方法可以解决此问题...

  1. Create a separate page (or ASHX handler preferably) which returns only the data for the image (no HTML or anything like that) and link to that page. 创建一个单独的页面(最好是ASHX处理程序),该页面返回图像的数据(不返回HTML或类似内容)并链接到该页面。
  2. Base-64 encode the byte array and include that as a data URI in the src attribute. Base-64对字节数组进行编码,并将其作为数据URI包括在src属性中。

There are lots of tutorials for the first option online. 在线上有很多关于第一个选项的教程。 This one was found by a quick Google search, there are others as well. 是通过Google的快速搜索找到的,还有其他搜索条件。 Essentially what the handler would do is accept an identifier on the query string, use that identifier to get the image from the database, then write the appropriate headers and content to the response. 从本质上讲,处理程序将执行的操作是接受查询字符串上的标识符,使用该标识符从数据库中获取图像,然后将适当的标头和内容写入响应。 The URL for the src attribute would then be that handler. src属性的URL将是该处理程序。 Something like: 就像是:

ImageUrl='<# "~/handler.ashx?id=" + Eval("id") #>'

(Or whatever your data-bound data uses as an identifier for the image.) (或者您的任何数据绑定数据用作图像的标识符。)

suppose your image column name in database "ImageName" then 假设您在数据库“ ImageName”中的图像列名称是

Solution 1:if your image in Root Folder 解决方案1:如果您的图片在根文件夹中

 <img src='<%#Eval("ImageName")%>' alt="" />

OR



 <asp:Image ID="Image1" ImageUrl='<%#Eval("ImageName")%>' runat="server" />

Solution 2:if your image in images Folder 解决方案2:如果您的图像在图像文件夹中

<img src='<%# "images/" + Eval("ImageName") %>'  alt=""/>

OR 要么

<asp:Image ID="Image1" ImageUrl='<%# "images/" + Eval("ImageName") %>' runat="server" />

Your Final Solution: 您的最终解决方案:

<asp:Repeater id="Rp1" runat="server">
           <ItemTemplate>
        <div class="item">
<img src='<%#Eval("ImageName")%>' alt="" />
OR
<asp:Image ID="Image1" ImageUrl='<%# "images/" + Eval("ImageName") %>' runat="server" />
        </div>
        </ItemTemplate>
        <footertemplate></footertemplate>
</asp:Repeater> 

You need to convert in URI for IMG HTML tag: 您需要在URI中为IMG HTML标签进行转换:

<img src="<%# System.Text.Encoding.ASCII.GetString(Eval("bynarydatacolumn")) %>" />

or equivalent. 或同等学历。

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

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