Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return image from controller without setting Content-Disposition tag?

So I'm using MVC 2 to serve up some images from a Controller Action. General wisdom (1, 2, 3) seems to be to use one of the FileResult types (FileStreamResult, FileContentResult or `FileContentResult').

All three concrete FileResult's, however, set the Content-Disposition response header with attachment; filename{YourFileNameHere}=UTF-8. The net result is that if a user views my image directly, rather than embedded in HTML, the browser present a save dialog, rather than displaying the image. I would prefer the image simply display in the browser.

I guess that makes my question: Using MVC 2 What is the appropriate way to return an image result where the image is NOT marked for download?

like image 457
EBarr Avatar asked Dec 02 '10 15:12

EBarr


People also ask

How do I display an image in razor view?

Step 1 - Go to SQL Server Management System and execute the following script. Step 2 - Go to Visual studio and add a new project. Select “Asp.Net MVC 4 Web Application” and give the name for this ,In my case it is “MVCDemoProject. ” -> Select a Project template as Empty and View engine is “Razor”, Then Click OK.

How do I return a controller model?

To return a model from the controller action method to the view, we need to pass the model/object in the View() method. Here, the View gets the model as UserNamePasswordModel object with its property set. To retrieve properties of this object in the view, we can use @Model ; like @Model.


1 Answers

The MVC framework uses the FileResult.FileDownloadName property to determine whether to send the content inline or as an attachment. If you're instantiating a FileResult-derived type directly, don't set its FileDownloadName property. Alternatively, if you're using the Controller.File factory methods, call an overload which doesn't take a fileDownloadName argument (or pass in null for that argument).

like image 177
Levi Avatar answered Sep 20 '22 15:09

Levi