Javascript:
let myFile = new Blob(["A file"], { type: "text/plain"});
fetch("server_location.php", {
method: "POST",
body: myFile,
headers: {
"Content-Type": "text/plain"
})
.then((res) => { return res.text(); }
.then((text) => { console.log(text) };
Php:
<?php
var_dump($_FILES)
Required
Should show the file in the file array
Output
array (0)
Please give me a solution so that I can upload custom text blob file created in javascript to my php server.
2
Answers
$_FILES
is populated when you upload a request formatted as Multipart Form Data and labeled with the current content-type.You are uploading a blob labelled as plain text, and stringifed (probably to
[object Object]
).To send a multipart request, use a FormData object.
To send plain text and read it:
and
If you are sending a file then "Content-Type" shouldn’t be "text/plain".
Set Content-Type to "multipart/form-data".