Get loaded block with PHP

I have a script that creates a blob and sends it to a PHP file. Here is my code:

HTML / JavaScript:

<script type="text/javascript"> function upload() { var data = new FormData(); data.append('user', 'person'); var oReq = new XMLHttpRequest(); oReq.open("POST", 'upload.php', true); oReq.onload = function (oEvent) { // Uploaded. }; var blob = new Blob(['abc123'], {type: 'text/plain'}); oReq.send(blob); } </script> <button type="button" onclick="upload()">Click Me!</button> 

PHP:

 <?php var_dump($_POST); ?> 

When I look at my developer console, I don't get any $ _POST data in my PHP page. I need to know how to get a text file sent in a PHP script.

Any help is much appreciated!

+6
source share
2 answers

Data from blob can be read with php://input , as in

 <?php var_dump(file_get_contents('php://input')); 

If you want to send multiple pieces of data using a form data object, it will look like a regular multipart / form-data column. The entire line will be available through $_POST and all drops and files through $_FILES .

 function upload() { var data = new FormData(); var oReq = new XMLHttpRequest(); oReq.open("POST", 'upload.php', true); oReq.onload = function (oEvent) { // Uploaded. }; var blob = new Blob(['abc123'], {type: 'text/plain'}); data.append('file', blob); oReq.send(data); } 
+8
source

Add your Blob to FormData and submit it.

0
source

Source: https://habr.com/ru/post/973270/


All Articles