I cannot figure out how to get blobs from XMLHttpRequest in my PHP script. I have code like this in JS:
var req = new XMLHttpRequest();
var file = document.getElementById('file').files[0];
req.open("POST", "uploader.php", true);
req.setRequestHeader("Content-Type", file.type); set request headers
req.setRequestHeader("Content-Length", file.size);
req.send(file);
if (req.readyState == 4 && req.status == 200) {
alert("result = " + req.responseText);
}
I understand how to send blob to the server, but I do not understand how and where to get it. I tried using global arrays $_POST, $_GETand $_FILES, but they have no files. I even tried adding filein url like this:
req.open("POST", "uploader.php?file=" + file, true);
This is not a solution.
Read the answers here. I found that I can get the source binary data using php://input. I tried and yes, I got some raw data. Transition to consideration of this decision through. But you want to ask, is this the only way? If not, what are the rest? And which one is most right?
, ("Content-Length", file.size), console.log : " " Content-Length ". ?