title: Ajax to Controller and return file in a IFrame
body: <p>I am trying to print the contents of an <code>byte[]</code> array into an Iframe and then also give the user the option to download the file in pdf format. </p>
  
  <p>In my view I have a button which hooked up to a <code>ajax</code> call which passes the <code>byte[]</code> to my actionresult which works and the return type is a <code>FileContentResult</code>. </p>
  
  <p>However am not sure how to return the File content in my <code>ajax</code> response and then set the source of the Iframe to the response I get back from the actionresult?</p>
  
  <p>View</p>
  
  <pre><code>&lt;input type="button" value="Show PDF" /&gt;
   &lt;div id="DisplayPDF"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Jquery / Ajax</p>
  
  <pre><code>$(document).ready(function () {
      $('input').on('click', function () {            
          $.ajax({
              url: '/BillPayment/GetPDF',
              dataType: 'json',
              type: 'POST',
              data: { id: '@Model.ROIAgreementApplication' }, //your string data
              success: function (response) {
                  $("#DisplayPDF").html(
                      $('&lt;iframe&gt;', {
                          src: this.href,
                          width: '600px',
                          height: "800px"
                      })
                  );
              }
          });
      });        
  });
  </code></pre>
  
  <p>Actionresult / controller</p>
  
  <pre><code>[HttpPost]    
  public ActionResult GetPDF(string id)
  {
      MemoryStream outputStream = new MemoryStream();
      MemoryStream workStream = new MemoryStream();
  
      var bytes = System.Text.Encoding.UTF8.GetBytes(id);
      byte[] byteArray = bytes;
      outputStream.Write(byteArray, 0, byteArray.Length);
      outputStream.Position = 0;
  
      return File(outputStream, "application/pdf");
  }
  </code></pre>
  
  <p>Earlier I was thinking of using <code>@Url.Action()</code> in the source attribute of a given Iframe like this</p>
  
  <pre><code>&lt;iframe src='@Url.Action("GetPDF","BillPayment", new { id = @Model.ROIAgreementApplication })' width="100%" height="1000" frameborder="0" /&gt;
  </code></pre>
  
  <p>However it doesn't even get passed to my actionresult?</p>
  
  <p>Any suggestions please?</p>