title: Returning a file to View/Download in ASP.NET MVC
body: <p>I'm encountering a problem sending files stored in a database back to the user in ASP.NET MVC. What I want is a view listing two links, one to view the file and let the mimetype sent to the browser determine how it should be handled, and the other to force a download.</p>
  
  <p>If I choose to view a file called <code>SomeRandomFile.bak</code> and the browser doesn't have an associated program to open files of this type, then I have no problem with it defaulting to the download behavior. However, if I choose to view a file called <code>SomeRandomFile.pdf</code> or <code>SomeRandomFile.jpg</code> I want the file to simply open. But I also want to keep a download link off to the side so that I can force a download prompt regardless of the file type. Does this make sense?</p>
  
  <p>I have tried <code>FileStreamResult</code> and it works for most files, its constructor doesn't accept a filename by default, so unknown files are assigned a file name based on the URL (which does not know the extension to give based on content type). If I force the file name by specifying it, I lose the ability for the browser to open the file directly and I get a download prompt. Has anyone else encountered this?</p>
  
  <p>These are the examples of what I've tried so far.</p>
  
  <pre><code>//Gives me a download prompt.
  return File(document.Data, document.ContentType, document.Name);
  </code></pre>
  
  <p></p>
  
  <pre><code>//Opens if it is a known extension type, downloads otherwise (download has bogus name and missing extension)
  return new FileStreamResult(new MemoryStream(document.Data), document.ContentType);
  </code></pre>
  
  <p></p>
  
  <pre><code>//Gives me a download prompt (lose the ability to open by default if known type)
  return new FileStreamResult(new MemoryStream(document.Data), document.ContentType) {FileDownloadName = document.Name};
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <hr>
  
  <p><strong>UPDATE:</strong>
  This questions seems to strike a chord with a lot of people, so I thought I'd post an update. The warning on the accepted answer below that was added by Oskar regarding international characters is completely valid, and I've hit it a few times due to using the <code>ContentDisposition</code> class. I've since updated my implementation to fix this. While the code below is from my most recent incarnation of this problem in an ASP.NET Core (Full Framework) app, it should work with minimal changes in an older MVC application as well since I'm using the <code>System.Net.Http.Headers.ContentDispositionHeaderValue</code> class.</p>
  
  <pre><code>using System.Net.Http.Headers;
  
  public IActionResult Download()
  {
      Document document = ... //Obtain document from database context
  
      //"attachment" means always prompt the user to download
      //"inline" means let the browser try and handle it
      var cd = new ContentDispositionHeaderValue("attachment")
      {
          FileNameStar = document.FileName
      };
      Response.Headers.Add(HeaderNames.ContentDisposition, cd.ToString());
  
      return File(document.Data, document.ContentType);
  }
  
  // an entity class for the document in my database 
  public class Document
  {
      public string FileName { get; set; }
      public string ContentType { get; set; }
      public byte[] Data { get; set; }
      //Other properties left out for brevity
  }
  </code></pre>