How to receive data sent from a browser using XMLHttpRequest on the server side (php)?

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();          // create instance

var file = document.getElementById('file').files[0];     // get File Object from <input type=file />. It is picture

req.open("POST", "uploader.php", true); // make request

req.setRequestHeader("Content-Type", file.type);   set request headers
req.setRequestHeader("Content-Length", file.size);

req.send(file);                 // send request with file-blob

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 ". ?

+3
2

:

ajax php

blob XmlHttpRequest, AJAX, , iframe.

, JQuery, .

, !

+1

:

req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
+1

All Articles