ASP.NET MVC 3: How to stream files to clients

Streaming files to the client is very easy using ASP.NET MVC 3: The following code snippet shows an exemplary controller action “Download” that streams data to the client. If the client requests this action (e.g. by using the link [YOUR_CONTROLLER]/Download) the browser will (depending on it’s settings) start downloading the data or open the download dialog.
The first optional parameter mimeType (e.g. “image/jpeg” for jpeg images or “application/pdf” for pdf documents) defines the HTTP header Content-type which is used by the browser to decide how to handle the data. The second optional parameter fileDownloadName defines the name of file the data should be saved to on the client’s computer.

public ActionResult Download()
  var fileStream = [...];
  var mimeType = [...]; // optional
  var fileDownloadName = [...]; // optional
  return File(fileStream, mimeType, fileDownloadName);

12 responses to “ASP.NET MVC 3: How to stream files to clients”

  1. If I wanted to send the contents of any particular “div” to an excel file, how would I define the filestream ?

    Thanks in advance for your response.

  2. Sorry, I do not understand your question. What do you mean with “contents of any particular div”? Would you like to transform the inner HTML of an div to an excel file?

  3. Hi,

    We are using similar thing for downloading a file. But after download at the client complete, we need to capture the event. How to do that?


  4. thanks for quick response. I actually need a notification on server once the download is complete.
    Once this code is executed, client side a dialog will open to save/cancel. Once the user clicks on save, the actual save may take some time if the file is bigger and during that time the server resources will be in use. We want to track the actual completion of the full file download and based on that we need to do some activity on the server side.

  5. I’m a little lost, can you explain what should go in the = […] part in more detail. An example would be great!

  6. Hi James,
    this depends on which type of content (var mimeType = […]) you want to send to the client and where the data come from (var fileStream = […]). As written in the post, the last (optional) parameter (fileDownloadName = […]) defines the default file name for the download on the client. To stream a pdf file to the client the code could look like this:

    public ActionResult Download()
      var fileStream = new FileStream(@"c:\PATH\TO\PDF\ON\THE\SERVER.pdf", FileMode.Open);
      var mimeType = "application/pdf";
      var fileDownloadName = "download.pdf";
      return File(fileStream, mimeType, fileDownloadName);

    I hope this helps …

  7. I have tried this an I keep getting a new View the says ‘undefined’ in it. If I reload that view the PDF opens fine the second time.

  8. Sometimes we don’t download a file that locates a directory on server. We creates a file on the fly and send it back to client. In, it’s easy to write information to HttpResponse.OutputStream after setup mimeType right.

    I am looking same way in MVC to do that.

  9. Nice tutorial simple and easy. It helped with a site for a friend of mine. Now i can build on from here.

  10. public ActionResult Download()
    var fileStream = […];
    var mimeType = […]; // optional
    var fileDownloadName = […]; // optional
    return File(fileStream, mimeType, fileDownloadName);

    in view:
    @section scripts {

    function callreport() {
    type: “POST”,
    url: ‘@Url.Action(“ReportViewer”, “Transaction”)’,
    data: { “id”: id },
    success: function (oPdf) {
    $(“#dialog-modal”).html(oPdf); //=> line of code in question?
    } window.onload = callreport;

    hi Jan Jonas, I need some help from u… how to display the return File(fileStream…) to a modal form via jquery? see line of code in question….

Leave a Reply

Your email address will not be published. Required fields are marked *