title: AJAX POST to download and upload PDF file
body: <p>I would like to write a chrome extension that downloads PDF file from a website accepting POST requests, and upload the PDF file to my localhost server. Here's my attempt:</p>
  
  <pre><code>  $.ajax({
          url: 'http://example.com/download.action',
          data: data,
          type: 'POST',
          cache: false,
          crossDomain: true,
          success: function(response) {
              $.ajax({
                  url: 'http://localhost/getpdf.php',
                  data: response,
                  type: 'POST',
                  cache: false,
                  contentType: 'application/octet-stream',
                  processData: false,
                  crossDomain: true
              });
          }
      });
  </code></pre>
  
  <p>From the console I observed the response of the download ajax request, it's a binary content beginning with "%PDF-1.7.%...", seems reasonable. Then in localhost server side, I use some simple PHP code to save the PDF file:</p>
  
  <pre><code>&lt;?php
  $raw_data = file_get_contents('php://input');
  $f = fopen('test.pdf', 'w');
  fwrite($f, $raw_data);
  fclose($f);
  ?&gt;
  </code></pre>
  
  <p>File is saved. But the saved PDF file can't be opened by Adobe Reader (file is damaged), and the file size is about 2 times larger than the original one.</p>
  
  <p>I checked the binaries of the saved PDF file and the original one by <code>vim -b</code>, here're the first 10 lines:</p>
  
  <p>The original one:</p>
  
  <pre><code>0000000: 2550 4446 2d31 2e37 0a25 e4e3 cfd2 0a36  %PDF-1.7.%.....6
  0000010: 2030 206f 626a 0a3c 3c2f 5479 7065 2f58   0 obj.&lt;&lt;/Type/X
  0000020: 4f62 6a65 6374 0a2f 5375 6274 7970 652f  Object./Subtype/
  0000030: 466f 726d 0a2f 4242 6f78 5b30 2030 2035  Form./BBox[0 0 5
  0000040: 3935 2e32 3736 2038 3431 2e38 395d 0a2f  95.276 841.89]./
  0000050: 5265 736f 7572 6365 733c 3c2f 584f 626a  Resources&lt;&lt;/XObj
  0000060: 6563 743c 3c2f 496d 3020 3720 3020 522f  ect&lt;&lt;/Im0 7 0 R/
  0000070: 496d 3120 3820 3020 522f 496d 3220 3920  Im1 8 0 R/Im2 9 
  0000080: 3020 523e 3e2f 436f 6c6f 7253 7061 6365  0 R&gt;&gt;/ColorSpace
  0000090: 3c3c 2f43 5330 2031 3020 3020 522f 4353  &lt;&lt;/CS0 10 0 R/CS
  </code></pre>
  
  <p>The saved one:</p>
  
  <pre><code>0000000: 2550 4446 2d31 2e37 0a25 efbf bdef bfbd  %PDF-1.7.%......
  0000010: efbf bdef bfbd 0a36 2030 206f 626a 0a3c  .......6 0 obj.&lt;
  0000020: 3c2f 5479 7065 2f58 4f62 6a65 6374 0a2f  &lt;/Type/XObject./
  0000030: 5375 6274 7970 652f 466f 726d 0a2f 4242  Subtype/Form./BB
  0000040: 6f78 5b30 2030 2035 3935 2e32 3736 2038  ox[0 0 595.276 8
  0000050: 3431 2e38 395d 0a2f 5265 736f 7572 6365  41.89]./Resource
  0000060: 733c 3c2f 584f 626a 6563 743c 3c2f 496d  s&lt;&lt;/XObject&lt;&lt;/Im
  0000070: 3020 3720 3020 522f 496d 3120 3820 3020  0 7 0 R/Im1 8 0 
  0000080: 522f 496d 3220 3920 3020 523e 3e2f 436f  R/Im2 9 0 R&gt;&gt;/Co
  0000090: 6c6f 7253 7061 6365 3c3c 2f43 5330 2031  lorSpace&lt;&lt;/CS0 1
  </code></pre>
  
  <p>It seems some words are changed (maybe charset problem?)</p>
  
  <p>Any hints about this?</p>