title: Send a file that I want to make and store in memory as IHttpActionResult
body: <p>I'm not sure what I want to do is even possible. </p>
  
  <p>I'm currently working on an AngularJS web app and need implement a way to take data from my database, store it into a <code>List&lt;object&gt;</code>, parse that <code>List&lt;object&gt;</code> into a .csv file and load it in memory, that way I can send it back as <code>HttpResponseMessage</code>. </p>
  
  <p>I already have all the data in a <code>List&lt;object&gt;</code> variable and will be using CSVHelper to parse that into a file, but all the examples I find online show me how to export the file locally with <code>File.WriteAllText()</code>, and they all use <code>File.OpenRead(filePath)</code> to read a file locally. </p>
  
  <p>How can I create and store the file in memory and send it back as a <code>HttpResponseMessage</code>?</p>
  
  <hr>
  
  <p><strong>EDIT:</strong></p>
  
  <p>After digging around I was able to come up with this, although it doesn't work. When I run the code I get "This site can't be reached. The Connection was rest.
  ERR_CONNECTION_RESET" from chrome. </p>
  
  <p>Hope this helps with visualizing what I'm trying to do. </p>
  
  <p><strong>The Javascript</strong></p>
  
  <pre><code>me.downloadFile = function (fileId) {
  
      $http({
          method: 'POST',
          url: 'api/notices/CheckFileExists',
          params: { fileId: fileId }
      }).then(function success(result) {
          if (result.data === 'Y') {
              me.downloadOpen("POST", 'api/notices/DownloadFile?fileId=' + fileId, "", "");
          }
          else {
              $mdToast.show(
                  $mdToast.simple()
                      .textContent(result.data)
                      .position("bottom right")
                      .hideDelay(2000)
              );
          }
      });
  };
  
  me.downloadOpen = function (verb, url, data, target) {
      var form = document.createElement("form");
      form.action = url;
      form.method = verb;
      form.target = target || "_self";
  
      if (data) {
          for (var key in data) {
              var input = document.createElement("input");
              input.name = key;
              input.value = typeof data[key] === "object" ? JSON.stringify(data[key]) : data[key];
              form.appendChild(input);
          }
      }
  
      form.style.display = 'none';
      document.body.appendChild(form);
      form.submit();
      document.body.removeChild(form);
  </code></pre>
  
  <p><strong>The C#</strong></p>
  
  <pre><code>[HttpPost]
      public HttpResponseMessage DownloadFile(int fileId)
      {
          var user = GetUser();
          var fileContent = NoticesMethods.GetNoticeFile(fileId, user);
  
          using (Globals.WebAppsImpersonator())
          {
              if (fileContent.Count &gt; 0)
              {
                  using (var mem = new MemoryStream())
                  using (var writer = new StreamWriter(mem))
                  using (var csvWriter = new CsvWriter(writer))
                  {
                      csvWriter.Configuration.Delimiter = ",";
                      csvWriter.WriteField("File ID");
                      csvWriter.WriteField("Account");
                      csvWriter.WriteField("Move Date");
  
                      foreach (var row in fileContent)
                      {
                          csvWriter.WriteField("\"" + row.FileId + "\"");
                          csvWriter.WriteField("\"" + row.Account + "\"");
                          csvWriter.WriteField("\"" + row.MoveDate + "\"");
                      }
  
                      writer.Flush();
  
                      HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
                      result.Content = new StreamContent(mem);
                      result.Content.Headers.ContentType = new MediaTypeHeaderValue("text/csv");
                      result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
                      result.Content.Headers.ContentDisposition.FileName = "Notice.csv";
  
                      return result;
                  }
              }
              return new HttpResponseMessage(HttpStatusCode.NotFound);
          }
      }
  </code></pre>