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 = "";
        }
    }
8
  • What really matters is the ado.net operations code. Commented Feb 14, 2017 at 1:31
  • @LeiYang I'm not sure I understand what you mean. Commented Feb 14, 2017 at 1:32
  • How can we talk about database without single line of related code? Commented Feb 14, 2017 at 1: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? Commented Feb 14, 2017 at 1:37
  • Why not search and try before you ask, such as 1 2 Commented Feb 14, 2017 at 1:39

1 Answer 1

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[]

Sign up to request clarification or add additional context in comments.

6 Comments

Would I add that when inserting the image to the database? or after? if after, where would you put that?
it will be after when you retrieve the image from database because it will convert from binary
if you are having issue you can paste the sql code for data retrival
The sql is nothing more than SELECT Image, ImageDesc FROM dbo.Images How would I set the value of img from the img field in the listview?
<%# Convert.ToBase64String((byte[])Eval("Image")) %> try this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.