views:

30

answers:

1

I'm using the following snippet of code in an MVC Action to return an image in an HTML image tag:

byte[] bytes = System.IO.File.ReadAllBytes(fileLocation);
string fileType = "application/octet-stream";
FileContentResult fcr = new FileContentResult(bytes, fileType);
fcr.FileDownloadName = fileName;
return fcr;

The image tag's src attribute looks like this: src="/MyController/MyAction/fileInfo"

It works and the image displays in the web page. However, if I try and open the image by itself in a browser the browser asks me what image viewer I should use to open this resources. If the image is statically linked off the site then the browser will open the image.

What do I need to change in that code to make the browser open this image?

(I've tried a few different content-result-types and also a few different file-types but haven't found the right combo yet.)

+3  A: 

You have to set the mime type of the response to the appropriate image type (ex: image/jpeg) instead of application/octet-stream.

munissor
I'd tried that previously and it hadn't worked. I now discovered that it doesn't work on Firefox but does work on IE, Chrome and Opera so I'm guessing that this might be a Firefox setting. When I was testing it I initially tested it on all browsers and then subsequent tests were just done on Firefox that's why I didn't catch it. Thanks again.
Guy