I have created FormData() that work very well, because i test it using jQuery.ajax. but cause i want to have progress bar for uploading, i could not create it when i use jQuery.ajax . so i decided to use ajax directly and post form data. but getting form data using php is not work.
javascript
xhr.open("POST","assets/php/upload.php?action=uploadFiles");
xhr.setRequestHeader('Cache-Control', 'no-cache');
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.setRequestHeader('Content-Type', 'multipart/form-data');
xhr.send(newFormData);
php
print_r($_FILES); //output, empty array
This is my question, why cannot i get form data using php?
EDIT::
this is my try to make progress bar using jquery but it did not work very well. the progress bar will be complete very fast and the file is not uploaded yet.
function updateProgress(evt)
{
// evt is an ProgressEvent.
if (evt.lengthComputable)
{
var percentLoaded = Math.round((evt.loaded / evt.total) * 100);
// Increase the progress bar length.
$(".progress > div").css(
{
width: percentLoaded + '%'
});
}
}
$.ajax(
{
url: 'assets/php/upload.php?action=uploadFiles',
type: 'POST',
data: newFormData,
cache: false,
xhr: function ()
{
myXhr = $.ajaxSettings.xhr();
if (myXhr.upload)
{
myXhr.upload.addEventListener('progress', updateProgress, false);
}
return myXhr;
},
contentType: false,
processData: false,
});
EDIT::
I finally found why i cannot get ajax request using $_FILES. that occurred because i set content type, now if i remove it from request, i can get ajax request. but now the progress bar will be complete soon while uploading file has not finished yet. where is the problem with my code?
thank you, Alireza