I am trying to upload file using AJAX in WordPress administration. Here is my code:
<script>
$(document).ready(function()
{
$("#upload_app").click(function(e)
{
event.stopPropagation();
event.preventDefault();
var data = new FormData();
aaa = document.getElementById("path_to_file");
alert(aaa.files[0]);
data.append("newFile",aaa.files[0]);
alert(data["newFile"]);
$.ajax({
url: "'.get_site_url().'/wp-content/plugins/move_and_match/upload_file.php",
type: "POST",
data: data,
cache: false,
dataType: "json",
processData: false,
contentType: false,
success: uploadDone,
error: uploadFailed
});
});
});
function uploadDone(returns)
{
alert(returns);
$("#uploadFileMetaBox p").text("Successfull.");
}
function uploadFailed(returns)
{
alert(returns);
$("#uploadFileMetaBox p").text("Problem occured.");
}
</script>
AJAX access PHP script without problems but no files are sent. As you can see I added some alerts to the script. First one alert(aaa.files[0]);
works without problems but the second one (when the value of aaa should be inserted into FormData object) says that the value is undefined
.
Here is jsfiddle
What am I doing bad?
Thanks for answers.