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);
}

Comments

  1. S

    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. Jan Jonas

    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. Swamy

    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?

    Thanks
    Swamy

  4. Jan Jonas

    Do you mean a client side / JavaScript event? IMHO, there is no native JavaScript event like “downloadComplete”.
    Maybe you can use a IFrame workaround as described here http://stackoverflow.com/questions/666468/ie6-detecting-when-the-file-download-popup-is-closed.

  5. Swamy

    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.

  6. James

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

  7. Jan Jonas

    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:PATHTOPDFONTHESERVER.pdf", FileMode.Open);
      var mimeType = "application/pdf";
      var fileDownloadName = "download.pdf";
      return File(fileStream, mimeType, fileDownloadName);
    }

    I hope this helps …

  8. Shamus

    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.

  9. Hong

    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 asp.net, it’s easy to write information to HttpResponse.OutputStream after setup mimeType right.

    I am looking same way in MVC to do that.
    Thanks.

  10. Jan Jonas

    Hi Hong,
    you can find a overload list for the File() method on http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.file.aspx. As you can see in the list, you do not need to pass a file stream as first parameter. You could for example use File(Byte[], String, String) and pass a (dynamically created) byte array as first parameter.

  11. Michael

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

  12. BertBartuline

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

    in view:
    @section scripts {

    function callreport() {
    $.ajax({
    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 comment

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

*