title: File Download Using Ajax and iframe
body: <p>I have read about doing a file download using Ajax and iframes.
  Can anybody give me a step by step explanation of how to do this or know of any tutorials seeing as we are already using ajax on this project this seems like the best way to do this.</p>
  
  <p>EDIT:</p>
  
  <p>Okay this is my view code:</p>
  
  <pre><code>&lt;div class="buttons"&gt;
  &lt;input type="button" value="Download File" class="button" id="downloadfile"&gt;
  </code></pre>
  
  <p></p>
  
  
  
  <pre><code>&lt;script type="text/javascript"&gt;
      $(function () {
      $('#downloadfile').click(function (e) {
          $('#downloadIframe').attr('src', '@Url.Action("DownloadFile","Invoice")' + '/Report/Invoices');
      });
  });
  &lt;/script&gt;
  </code></pre>
  
  <p>This is my controller:</p>
  
  <pre><code>public FileResult DownloadFile(int id)
      {
  
          byte[] fileBytes = System.IO.File.ReadAllBytes(Server.MapPath("~/Reports/Invoices/" + Table.First(x =&gt; x.ID == id).ID + ".pdf"));
          string fileName = Table.First(x =&gt; x.ID == id).ID.ToString();
          return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Pdf, fileName);
      }
  
      public ActionResult Download(int id)
      {
          return AjaxResponse("Download", null);
      }
  </code></pre>
  
  <p>I have a <code>Jquery</code> context menu that I use to click on a row in a <code>JQGrid</code> and then open the view <code>Download</code> and then in the view I click the button and it should execute the <code>script</code> in the view and return <code>DownloadFile</code> <code>FileResult</code> in the controller but nothing happens when I click the button.</p>
  
  <p>The folder where the reports are: <code>~/Reports/Invoices</code></p>