I'm trying to upload a (DICOM) binary file to the server using XMLHttpRequest and FileReader.
According to DICOM-standard as Content-Type muss be multipart/related;type=application/dicom defined and in the Request Payload must be the Content-Type:application/dicom again, I have managed to build that structure somehow with this 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);
};
The problem with that approach is that it seems that XMLHttpRequest makes an UTF-8 encoding and that's corrupting the binary data (see this post).
- The questions are how can use reader.readAsArrayBuffer() and set the body content-type and boundary? or
- How can I prevent XMLHttpRequest to make UTF-8 encoding?
My second question is how to handle the big files (about 1 TB) with this approach?