title: Binary XHR result to File blob - Jquery
body: <p>Web service to donwload a document returns the following:</p>
  
  <pre><code>%PDF-1.5
  %����
  1 0 obj
  &lt;&lt;/Type/Catalog/Pages 2 0 R/Lang(en-GB) /StructTreeRoot 14 0 R/MarkInfo&lt;&lt;/Marked true&gt;&gt;&gt;&gt;
  endobj
  2 0 obj
  &lt;&lt;/Type/Pages/Count 1/Kids[ 3 0 R] &gt;&gt;
  endobj
  3 0 obj
  &lt;&lt;/Type/Page/Parent 2 0 R/Resources&lt;&lt;/Font&lt;&lt;/F1 5 0 R/F2 8 0 R&gt;&gt;/ExtGState&lt;&lt;/GS7 7 0 R&gt;&gt;/ProcSet[/PDF
  /Text/ImageB/ImageC/ImageI] &gt;&gt;/MediaBox[ 0 0 595.32 841.92] /Contents 4 0 R/Group&lt;&lt;/Type/Group/S/Transparency
  /CS/DeviceRGB&gt;&gt;/Tabs/S/StructParents 0&gt;&gt;
  endobj
  4 0 obj
  </code></pre>
  
  <p>To convert this data to file, i'm using the following code:</p>
  
  <pre><code>$.ajax({
        url: "/documents/docDownload",
        type: "GET",
        async:   true,
        headers: {
          responseType: "arraybuffer"
        },
        success: function(data,status,xhr) {
          var file = new Blob([data], {type: 'application/pdf'});
          var fileURL = window.URL.createObjectURL(file);
          var a = document.createElement("a");
          a.href = fileURL;
          a.download = data.name || "newPDF2";
          document.body.appendChild(a);
          a.click();
          $(window).on('focus', function(e) {
            $('a').remove();
          });
        }
  })
  </code></pre>
  
  <p>The functions returns me a pdf file, however when i open the pdf file, it's empty, seems that during the conversion was lost information.</p>
  
  <p>Anybody knows why is it happening?</p>
  
  <p>PHP - Web-Service code</p>
  
  <pre><code>$path = "/xxx/xxx/xxx/xxx/work.pdf";
  $res = $app-&gt;response();
  $res['Content-Description'] = 'File Transfer';
  $res['Content-Type'] = 'application/force-download';
  $res['Content-Type'] = 'application/pdf';
  $res['Content-Disposition'] ='attachment; filename=' . basename($path);
  $res['Content-Transfer-Encoding'] = 'binary';
  $res['Expires'] = '0';
  $res['Cache-Control'] = 'must-revalidate';
  $res['Pragma'] = 'public';
  $res['Content-Length'] = filesize($path);
  readfile($path);
  </code></pre>