I'm currently trying to view the images that are stored in the database as a BLOB, to do this, I created a Controller Action of type FileContentResult, which looks like this:
public FileContentResult ImageFile(string imageId) { var client = new WcfTestClient(); DataTable table = client.GetImageData(imageId); var image = ConvertToBytes(table); byte[] bytes; bytes = image[0].Values.ElementAt(0); return File(bytes, @"image/png"); }
It gets the correct image data from the database, and ConvertToBytes () works correctly, but for some reason, after returning the image to the view, it goes back to the beginning and starts again.
I use Ajax to call a method from my view as follows:
$.ajax({ url: "/Home/ImageFile", type: "GET", data: { "imageId": imageId }, success: function (data) { var image = document.createElement('img'); image.src = "/Home/ImageFile?id=" + imageId; image.width = 100; image.height = 100; div.appendChild(image); } });
Does anyone know what might cause my problem?
Update
Ok, now, after I was told that it was Ajax, it messed it up, I tried to send the parameter to my Controller action as follows:
var images = JSON.parse(window.localStorage.getItem("JsonImages")); var div = document.getElementById('imgOutput'); for (var i = 0; i < images.length; i++) { var imageId = images[i].pi_Id; var imgId = JSON.parse('{"imageId":' + imageId + '}'); var img = document.createElement('img'); img.src = "/Home/ImageFile?id=" + imgId; img.width = 100; img.height = 100; div.appendChild(img); }
Which, unfortunately, does not work very well. So, is there a way to send the ImageFile () parameter without running it twice? Am I missing something here?
Update 2
Finally, I got him to work! Here's what it looks like now:
var images = JSON.parse(window.localStorage.getItem("JsonImages")); var div = document.getElementById('imgOutput'); var createImage = function (src) { var img = document.createElement('img'); img.src = src; img.height = 100; img.width = 100; return img; } for (var i = 0; i < images.length; i++) { var imageId = images[i].pi_Id; var imageSrc = "/Home/ImageFile?imageId=" + imageId; div.appendChild(createImage(imageSrc)); }
I just needed to change the original parameter from id to imageId (duh). Thank you for your help!