title: Download File from FileResult without saving to disk
body: <p>I am using MVC 4.</p>
  
  <p>I have a method in my controller which generates a CSV file on demand and I want this file to be then downloaded by the user without the need of saving it to the disk at the server side. So I am passing a MemoryStream on the File() object to avoid having to firstly save file to disk and later give him the path for download.</p>
  
  <p>Controller Method:</p>
  
  <pre><code>[HttpGet]
  public ActionResult GenerateCsv(string data)
  
  
      var sb = new StringBuilder();
      sb = GetCsvReportToString(data);
  
      var stream = new MemoryStream(Encoding.UTF8.GetBytes(sb.ToString()));
  
      return this.File(stream, "text/csv", "ReportCsv.csv");
  }
  </code></pre>
  
  <p>sb (StringBuilder) has the correct data, as I have debugged it and confirmed it.</p>
  
  <p>my javascript code:</p>
  
  <pre><code>window.open(generateFileLink + '?data=' + dataToSend, '_blank');
  </code></pre>
  
  <p>The window gets in fact opened, the download dialog appears but with an error, saying that it cannot be downloaded from localhost:</p>
  
  <p><img src="https://i.stack.imgur.com/GQfWh.png" alt="enter image description here"></p>
  
  <p>The name is different in the download "ReportToCsv" because I didn't use real names for methods in my question, but doesn't matter.</p>
  
  <p>Can anyone assist? </p>