The ASP.NET MVC RC includes a number of file handling
enhancements:
FileResult and File() helper method
The RC build adds a new FileResult class that is used to
indicate that a file is being returned as an ActionResult from a Controller
action method. The Controller base class also now has a set of File()
helper methods that make it easy to create and return a FileResult.
For example, let’s assume we are trying to build a photo
management site. We could define a simple “Photo” class like below that
encapsulates the details about a stored Photo:
Figure 38
We could then use the new File() helper method like below to
implement a “DisplayPhoto” action method on a PhotoManager controller that
could be used to render the Photo out of a database store. In the code
below we are passing the File() helper the bytes to render, as well as the
mime-type of the file. If we pointed a <img src=””/> element at our
action method URL the browser would display the photo inline within a page:
Figure 39
If we wanted an end-user to be able to download the photo
and save it locally, we could implement a “DownloadPhoto” action method like
below. In the code below we are passing a third parameter – which will
cause ASP.NET MVC to set a header that causes the browser to display a “Save
As…” dialog which is pre-populated with the filename we’ve supplied:
Figure 40
When a user clicks a link to the
/PhotoManager/DowloadPhoto/1232 URL they’ll be prompted to save the picture:
Figure 41