views:

90

answers:

2

i have a link

<a id="DownloadLink" href='controller/action' target="_blank">Download File</a>

that has to open a image in another tab or page. but it offers me to download the file.
my action looks like this

public FileContentResult GetSurveyFile(int Id)
        {
            if (Id == 0)
                return null;
            Survey survey = Repository.GetItem(Id);
            if (survey.File == null)
                return null;

            return File(survey.File.FileContent.ToArray(), survey.File.ContentType,survey.File.Name);
        }

where the content type is an image/jpeg

whats wrong?

+5  A: 

How a browser handles image files is up to the browser/OS/user preferences. To absolutely guarantee that it is opened and not downloaded, simply wrap the image in and html page where the image is the only thing in the body.

Russell Leggett
+2  A: 

Nothing is wrong with this - if you send data to a browser with a mime type set, it may decide to offer a download rather than display the content. Most browsers are set up to download MP3 files, for example.

In your scenario, the browser is deciding to download the image.

You can display it by embedding it inside of an HTML page - this will ensure it displays in all browsers, rather than downloading - and the user can use the normal methods to save the image from the web page if they wish.

...
<body>
    <img src="your-c#-page" />
</body>
....
Sohnee