I am using this code to POST binary data to a REST interface:
var file = $("#file-to-upload")[0].files[0];
var filename = file.name;
var url = "/upload";
const reader = new FileReader();
reader.onload = function(e) {
console.log(`Read file data: length: ${e.target.result.length}`);
$.post({
url: url,
data: e.target.result,
headers: {
"Content-Type": "application/octet-stream",
"Content-Disposition": `attachment; filename=${filename}`
},
success: function(r) {
console.log(r);
}
}).fail(function(xhr, status, error) {
console.log(xhr);
});
}
reader.readAsBinaryString(file);
The console correctly says the number of bytes in the file. But when checking the actual POST in the development tools, I can see it sends more data:
e.target.result is a binary string, not serialized (strangely enough the dev console says 26.8kB, while the string length is 12817).
What am I missing here?



e.target.resulte.target.resultis a binary string which is exactly the file I uploaded (see updated question). Data is not serialized (that is kind of the point).data: { binary: e.target.result },.