C# list/array of Images to JS


Joined
Nov 23, 2021
Messages
1
Reaction score
0
I have c# API where download image from SQL and convert to Image class object and return Image[], after that i want show my images in JS, and save to local storage, but images not showing, because src of images are [object Object] JavaScript images src. How can extract image from Array?

API C#

C#:
try
        {
            List<Image> photoList = new List<Image>();
            sqlConnection.Open();
            byte[] imgArray;
            Image fullImage;
            dr = command.ExecuteReader();
            int i = dr.FieldCount;
            if (dr.HasRows)
            {
                while (dr.Read())
                {
                    imgArray = (byte[])dr["Image"];
                    fullImage = ByteArrayToImage(imgArray);

                    photoList.Add(fullImage);
                }
            }
            Image[] imagesArray;
            imagesArray = photoList.ToArray();
            return imagesArray;
        }

AJAX REQUEST
JavaScript:
$.ajax({
    type: 'GET',
    url: '/api/fileupload/download' + "?" + "name=" + name,
    timeout: 0,
    success: function (response) {
        console.log(response);
        for (var i = 0; i < response.length; i++) {
            console.log(response[i]);
            var Img = document.createElement('img');
            Img.src = response[i];
            parent.appendChild(Img);
        }
    },
    error: function (err) {
        console.log(err);
    }
})
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top