0

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";
                 }
            ?>
3
  • That should work, assuming $('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? Commented Mar 21, 2013 at 15:50
  • Why is the file input not inside the form in the first place? Commented Mar 21, 2013 at 15:53
  • edit, now working ok! Commented Mar 21, 2013 at 16:39

2 Answers 2

1

I think you mean that the element

$("#userDroppedFile")

Is your drop area.

That's not how you get the file though, you get it in the drop event:

$("#userDroppedFile").on("drag dragend dragenter dragleave dragover dragstart drop", function(e){
    var files;
    e.preventDefault();

    if( e.type === "drop" && e.originalEvent.dataTransfer ) {
        files = e.originalEvent.dataTransfer.files;
    }

    if( !files || !files.length ) {
        return;
    }

    doUpload(files);

});

In doUpload you take form data from your static html form, and add the files into it and upload:

function doUpload(files) {

    var formData = new FormData($("form")[0]);

    for( var i = 0; i < files.length; ++i ) {
        var file = files[i];
        formData.append("file" + (i+1), file);
    }

    return $.ajax({
        url: "xx",
        type: "POST",
        data: formData,
        processData: false,
        contentType: false,
        success: function() {
        }

    });
}
Sign up to request clarification or add additional context in comments.

12 Comments

no, i tried to simply thins, thast my drop area, but im catching the file, if you want i cant post all the code, but assuming that i already have the file, how can i attach it to a form. thanks for the fast answer, ill try!
@SantiagoNicolasRoca I am showing how to attach it to a form in the doUpload function.. you cannot attach a file to the normal form, you can only use ajax to upload it unfortunately.
thats even better, ive already tried that and the php that upload the files its still receiving an empty $_FILES
@SantiagoNicolasRoca there could be a problem using file.name as the file entry key. I will edit a possible fix, other than that I don't know without any debug information.
@iPadDeveloper2011 use processData: false and contentType: false
|
0
var myFile = $('userDropedFile');

Not a valid jQuery selector. Do you perhaps mean:

var myFile = $('#userDropedFile');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.