I'm working on an drag n drop functionality. I have the code that I need to catch the file when it's dropped on my drop area, but I'm have some issues to send it for xmlhttprequest since I cant send through jQuery or ajax.
I'm now thinking about a hidden iFrame, but I don't really know how I can attach this file, that I already have, thanks to the user drop, to an existing form.
var droparea = $('#dropzone');
var uploadButon = $('#upload');
droparea.on('dragover',function(){
$(this).addClass('dragover');
return false;
});
droparea.on('dragleave',function(){
$(this).removeClass('dragover');
return false;
});
droparea.on('drop', function(e) {
e.stopPropagation();
e.preventDefault();
$(this).removeClass('dragover');
var files = e.originalEvent.dataTransfer.files;
var formData = new FormData($("form")[0]);
for( var i = 0; i < files.length; ++i ) {
var file = files[i];
formData.append("files[]", file);
}
return $.ajax({
url: "upload.php",
type: "POST",
data: formData,
processData: false,
contentType: false,
success: function(text) {
alert(text);
}
});
});
//the php
<?php
echo count($_FILES["files"]);
for($i = 0; $i < count($_FILES["files"]["name"]) ; $i++)
{
if(move_uploaded_file($_FILES["files"]["tmp_name"][$i], "upload/".$_FILES["files"]["name"][$i]))
echo "ok";
else
echo "no";
}
?>
$('userDropedFile')is targeting a file input. However, if you're using drag-n-drop file upload, why not go a little further and also implement xhr file upload rather than using an iframe if it's available?