1

I dont know what do I have to do. Here's my code when searching the image:

public ActionResult SearchImage() {    
    var path = @"\\jesus-pc\Frontera\IMAGENES\SINGNOS DISTINTIVOS\0\80HP23891268272.TIF";
    FileStream stream = new FileStream(path,FileMode.Open,FileAccess.Read);
    byte[] data = new byte[(int)stream.Length];
    stream.Read(data,0, data.Length);

    return Json(new { base64image = Convert.ToBase64String(data) }, JsonRequestBehavior.AllowGet);
}

I do this by ajax:

$(".doc").dblclick(function () {                  
    $.ajax({
        url: "mainpage/SearchImage",
        contentType: 'application/json',
        dataType: "json",
        type: "get",
        success: function (data) {
            ModalWindow.open();
            document.getElementById("img_1").src = 
                "data:image/jpeg;base64"+data.base64image;
        }

})

But here's what I get in browser:

{ base64image: "SUkqAEgAAABXQU5HIFRJRkYgAQAwAAAAVGl0bGU6AEF1dGhvcjoAU3ViamVjdDoAS2V5d29yZHM6AENvbW1lbnRzOgAAAAAAEgD+AAQAAQ... AAFXEAAA==" }

von v.
  • 16,868
  • 4
  • 60
  • 84
  • You may need to convert it to an image rather than just reading the file text: http://www.dailycoding.com/Posts/convert_image_to_base64_string_and_base64_string_to_image.aspx – cwharris Apr 18 '13 at 13:53
  • Ignore @Nolonar, he clearly does not understand the Image tag's ability to interpret `data:image` sources. – cwharris Apr 18 '13 at 13:55
  • You may also need a `,` after `data:image/jpeg;base64`. Furthermore, you're pulling in a TIF file, and `img` `src` is expecting jpeg data. – cwharris Apr 18 '13 at 13:57
  • @ChristopherHarris it doesnot work, I've been dealing with this 2 day and the same sh#$ happend over and over again. Does not matter what code i use, the same i get in browser. And yesterday i tried the code of the site you post and it did not work neither. –  Apr 18 '13 at 13:59
  • when retorning image object i get the error of cannot return image in mvc.actionresult. If i change it to mvc.Image, nothing is being displaying. –  Apr 18 '13 at 14:01
  • Couple of questions: What browser are you using? Can you post the raw html of the img tag *after* the src is set? – cwharris Apr 18 '13 at 14:38
  • Im using chrome and here the img tag –  Apr 18 '13 at 14:40
  • I can save the image(http://stackoverflow.com/questions/16083599/how-to-encode-an-image-to-display-it-in-browser) but i cannot render it in browser. –  Apr 18 '13 at 14:42
  • The `src` is null, are you sure this is what the html looks like *after* you've updated the source? (use chromes developers tools to get the html) – cwharris Apr 18 '13 at 14:59
  • no, once a updated the src i get –  Apr 18 '13 at 15:01
  • That makes no sense. the `src` is suppose to be a base 64 encoded string representing an image, not a URI to a resource on your local server. Are you sure that's the correct img tag? – cwharris Apr 18 '13 at 15:02
  • I've cheack thousands times, i've even changed even the src attribute with javascript and it works perfectly, the image is being found was well but not being display. –  Apr 18 '13 at 15:05
  • OH, wow. Now I see what you're trying to do. Hold on. – cwharris Apr 18 '13 at 15:46

1 Answers1

0

I believe This is what you're looking for.

Can an ASP.NET MVC controller return an Image?

This has nothing to do with base 64 encoding your image. Base 64 encoding does work, if that's what you want, but it looks like you just want to deliver an image from the server to be rendered in the client. You can do that easily by using a FileResult.

In your case, your image's src tag should just be src="mainpage/SearchImage", and your existing code-behind should be replaced with something like the following:

public ActionResult SearchImage(string id)
{
    var path = @"\\jesus-pc\Frontera\IMAGENES\SINGNOS DISTINTIVOS\0\80HP23891268272.TIF";
    return base.File(path, "image/tiff");
}
Community
  • 1
  • 1
cwharris
  • 17,835
  • 4
  • 44
  • 64