I have a script that is creating a blob and posting 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 am not getting any $_POST data on my PHP page. I need to know how to retrieve the text file being posted to PHP script.
Any help is greatly appreciated!
The data from the blob can be read from php://input
, as in
<?php
var_dump(file_get_contents('php://input'));
If however you want to send multiple pieces of data with a form data object it would be like a normal multipart/form-data post. All string would be available through $_POST
and all blobs and file 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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With