title: ASP.net WebAPI return a file through ajax call
body: <p>I am currently looking for a way to force a download of a file which is returned through a WebAPI controller.</p>
  
  <p>I was using <a href="http://www.shawnmclean.com/blog/2012/04/force-download-of-file-from-asp-net-webapi/" rel="nofollow">http://www.shawnmclean.com/blog/2012/04/force-download-of-file-from-asp-net-webapi/</a> as a reference.</p>
  
  <p>On my client I am using an ajax GET call to send up an ID of an object and try to download the file</p>
  
  <pre><code>    exportList: (foo, callback) =&gt;
          path = '/api/export/id'
          path = path.replace("id", foo.id)
          $.ajax(
              url: path,
              dataType: 'text',
              success: (data) =&gt;
                  callback(data)
              error: (data) =&gt;
                  callback(false)
          )
  </code></pre>
  
  <p>On the server-side, I have the above URI routed to the method below</p>
  
  <pre><code>    [AcceptVerbs("GET")]
      public HttpResponseMessage ExportList(int id)
      {
          string file = fooService.ExportList(id);
  
          if (file == null)
          {
              return Request.CreateResponse(HttpStatusCode.NoContent);
          }
          HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
          result.Content = new StringContent(file);
          result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
          result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
          result.Content.Headers.ContentDisposition.FileName = "List.csv";
  
          return result;
      }
  </code></pre>
  
  <p>The fooService.ExportList method simply creates a csv string. </p>
  
  <p>When watching the request come back to the client, the response does have the csv string in it, but the client is not prompted or forced to download it.  </p>
  
  <p>Is this the correct way to go about this?</p>