Skip to content
Advertisement

Upload File Error with AJAX Form in WordPress – Specified file failed upload test

I am stuck since couple of days on the same issues, i hope someone can help with this. I have tried mutiple functions i found on stackoverflow but it seems i am doing something completely wrong!

I need to upload a file to wordpress library with a form submission on the frontend using AJAX.

This is the form:

JavaScript

AJAX call:

JavaScript
JavaScript

So the form data is showing up in the email like following after form submission:

JavaScript

I am not getting the data of the imagefile field. and that’s why i guess i am getting an error Specified file failed upload test.

Whats wrong with my function.. i have by the way tried $_FILES / $_POST but there is something i am missing.

EDIT JS File

JavaScript

FORM

JavaScript

PHP

JavaScript

Advertisement

Answer

When using the FormData object in $.ajax, you pass it directly as the data field

JavaScript

In PHP

$_FILES['imagefile'] will give you access to the file
$_POST['message'] will give you access to the textarea text
$_POST['_nonce'] will give you access to the nonce
$_POST['action'] will give you access to the action

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement