0

I have a page to upload images to an image column in SQL Server and it converts it to binary... On another page I want to retrieve the images. I'm trying to do it like this in a listview:

<ItemTemplate>
    <tr style="">
        <td>
            <img src='data:image/jpg;base64,<%# Eval("Image") %>' />
        </td>
        <td>
            <asp:Label ID="ImageDescLabel" runat="server" Text='<%# Eval("ImageDesc") %>' />
        </td>
    </tr>
</ItemTemplate>

Am I doing something wrong? I figured it would be the best way to go about this.

The Image save code looks like this:

    void InsertImage()
    {
        byte[] theFile = new byte[FileUpload1.PostedFile.ContentLength];
        HttpPostedFile file = FileUpload1.PostedFile;
        file.InputStream.Read(theFile, 0, (int)FileUpload1.PostedFile.ContentLength);

        if (FileUpload1.PostedFile != null && FileUpload1.PostedFile.FileName != "")
        {
            int result = new _Image()
                {
                    ImageName = txtFileName.Text,
                    ImageFile = theFile,
                    ImageDesc = txtDescription.Text
                }.AddImage();

            txtFileName.Text = "";
            txtDescription.Text = "";
        }
    }
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Thomas Colbert
  • 152
  • 1
  • 15
  • What really matters is the **ado.net** operations code. – Lei Yang Feb 14 '17 at 01:31
  • @LeiYang I'm not sure I understand what you mean. – Thomas Colbert Feb 14 '17 at 01:32
  • How can we talk about database without single line of related code? – Lei Yang Feb 14 '17 at 01:34
  • What other code is necessary? I will post it. I need to convert from Image datatype from SQL Server to an image in the listview. Ive see where people do with data:image/jpg;base64, + binary data, so my question is, if this is retrieving binary data from the Image column, why doesn't it work the same way, am I missing something in the src= part? – Thomas Colbert Feb 14 '17 at 01:37
  • Why not search and try before you ask, such as [1](http://stackoverflow.com/questions/698912/asp-net-store-image-in-sql-and-retrieve-for-aspimage) [2](http://stackoverflow.com/questions/14935205/retrieve-image-from-database-in-asp-net) – Lei Yang Feb 14 '17 at 01:39
  • @LeiYang Those don't really apply to using it in a listview... Ive tried doing it that way. – Thomas Colbert Feb 14 '17 at 01:49
  • can you add the code of data save and retrieval and also what is they type of column of image in sql – Usman Feb 14 '17 at 01:53
  • @Usman Updated it with the save code. the type of column in sql is image and the retrieval is what I'm trying to figure out. I was hoping it would be as simple as what I posted originally but I'm running into problems with that. – Thomas Colbert Feb 14 '17 at 02:00

1 Answers1

0

You are trying to use byte[] instead of base64 string

data:image/jpg;base64,<%# Eval("Image") %>  

in this line Eval("Image") will return a byte array which is not supported by html you can you Convert.ToBase64String(img) img should be byte[]

Usman
  • 4,615
  • 2
  • 17
  • 33