I am trying to post a file and regular key value pair data together using ajax. But it seems I can't get both through at the same time. How to achieve this?
var settingsData = new FormData();
var file = $("#background")[0].files[0];
settingsData.append("background", file, file.name);
settingsData.append("foo", "bar");
$.ajax({
url: 'https://myurl.com',
data: settingsData,
contentType: false,
processData: false,
type: "POST"
})
.done(function (data) {
console.log(data);
})
.fail(function (fail) {
console.log('Error', fail);
});
If I have files I can go though them like this:
foreach ($this->request->getUploadedFiles() as $file){
echo $file->getName(), " ", $file->getSize(), "\n";
}
But if I add key value pairs with the file I get nothing through. Why isn't FormData coming through so I could access it using json_decode(file_get_contents('php://input') ? Also $this->request->getPost("myVar") is empty.