1

I have a lot of images stored in a folder on the server side. The database has the URL of each image. I cannot open the image directly from the client side because the client side does not have permission to do it.

The image is being searched by an AJAX call and when the user clicks the bottom, a modal window fades in and so should the image, but it doesn't. I was not sure if the images could be found so I used console.log to see what value it's getting and a lot of symbols are being returned which makes me think that it is because of the file format.

Here's the AJAX call:

 $.ajax({
            url: "mainpage/SearchImage",
            data: "",
            type: "post",
            success:function(data){

                $("#img_1").css("src","data:image/tiff;base64,"+data);
                console.log(data);

            }

        })

The actionresult that searches for the images is:

public ActionResult SearchImage() {
            var path = @"\\jesus-pc\Frontera\IMAGENES\SINGNOS DISTINTIVOS\0\75HP23891268272.TIF";//the url is static cuz it just a test to know how to display the image

            var img = base.File(path, "image/tiff");//this file format is valid, right?

            return img;
        }

But as I said, it can find it, but it prints out a lot of symbols, not in the #img_1 img tag but in the console.

How can I fix this?

I'm using MVC 4 and C#.

ryan1234
  • 7,237
  • 6
  • 25
  • 36
  • See this questions: http://stackoverflow.com/questions/186062/can-an-asp-net-mvc-controller-return-an-image – jle Apr 17 '13 at 18:23
  • If you look carafully, the code i have is the same. The problem is when displaying the image. Do I have to specify the file format? Or what is that i have to do to display the image correctly? –  Apr 17 '13 at 18:30
  • You don't need an AJAX call, just replace your JavaScript code with: `$('#img_1').attr('src', 'mainpage/SearchImage');`. MVC is already returning the plain image, so all you have to do is to set the `src` attribute of your `img` tag. – Paolo Moretti Apr 17 '13 at 18:36
  • I reciving the image bytes but i cannot generate it. I mean, i just can see alot of symbols in the img tag but not the image. What could be the cause?. –  Apr 17 '13 at 20:37
  • You don't need to load the image using a [data URI](https://en.wikipedia.org/wiki/Data_URI_scheme), because your image is not a [base64 serialized image](https://en.wikipedia.org/wiki/Data_URI_scheme#Examples). The `src` tag has to be the URI for the image to be embedded, i.e. `mainpage/SearchImage`. Edit your view add this: ``, and the image should be displayed. – Paolo Moretti Apr 18 '13 at 11:19

0 Answers0