title: Upload binary file using XMLHttpRequest and FileReader whyle setting custom content-type and boundary
body: <p>I'm trying to upload a (DICOM) binary file to the server using XMLHttpRequest and FileReader.</p>
  
  <p>According to DICOM-standard as Content-Type muss be <code>multipart/related;type=application/dicom</code> defined and in the Request Payload must be the <code>Content-Type:application/dicom</code> again, I have managed to build that structure somehow with this code:</p>
  
  <pre><code>    let boundary = Math.random().toString().substr(2);
      let reader = new FileReader();
      reader.readAsBinaryString(fileList[0]);
  
      reader.onload = function(e) {
  
          var xmlHttpRequest = new XMLHttpRequest();
          xmlHttpRequest.open("POST", "myurl", true);
          var dashes = '--';
          var crlf = "\r\n";
          if ( fileList[0].type == '' ){
              filetype = 'application/dicom';
          } else {
              filetype = fileList[0].type;
          }
          let content = e.target["result"];
          var data = dashes + boundary + crlf + "Content-Type: " + filetype + crlf + crlf + content + crlf + dashes + boundary + dashes;
          xmlHttpRequest.setRequestHeader("Content-Type", "multipart/related;type=application/dicom;boundary=" + boundary+";");
          xmlHttpRequest.setRequestHeader("Accept", "application/dicom+json");
          xmlHttpRequest.send(data);
      }; xmlHttpRequest.send(data);
          };
  </code></pre>
  
  <p>The problem with that approach is that it seems that XMLHttpRequest makes an UTF-8 encoding and that's corrupting the binary data (see <a href="https://log.koehr.in/2017/04/09/the-magic-0xc2" rel="nofollow noreferrer">this</a> post).</p>
  
  <ul>
  <li>The questions are how can use reader.readAsArrayBuffer() and set the body content-type and boundary? or</li>
  <li>How can I prevent XMLHttpRequest to make UTF-8 encoding?</li>
  </ul>
  
  <p><strong>My second question is how to handle the big files (about 1 TB) with this approach?</strong></p>