5

I'm a java guy trying my hand in JavaScript and need some help. I came across an amazing tutorial on image uploads here Mozilla Tutorial and need some help figuring it out. I am currently working on the drag and drop image upload feature. Every time I drag an image onto my area the mouse turns green so it's activated. But then when I let go it should send me an alert that says one image was found. However it always just alerts 0. So the size of the array is 0. Any ideas? Thanks for taking a look. What I've tried with no success...

  1. Copying and pasting the code from the tutorial into my JavaScript file exactly
  2. Moving the code to add the listeners outside of a function and into a window onload
  3. Every browser I have

...

function toggleStrideMedia()
{
    if(getDisplay("strideMediaWrapper") == "" || getDisplay("strideMediaWrapper") == "none")
    {
        show("strideMediaWrapper");
             
        getElement("strideMediaDropZone").addEventListener("dragenter", dragenter, false);
        getElement("strideMediaDropZone").addEventListener("dragover", dragover, false);
        getElement("strideMediaDropZone").addEventListener("drop", drop, false);
    
    }
    else
    {
        hide("strideMediaWrapper");
    }
}

function dragenter(e) 
{
    e.stopPropagation();
    e.preventDefault();
}
     
function dragover(e) 
{
    e.stopPropagation();
    e.preventDefault();
} 

function drop(e) 
{
    e.stopPropagation();
    e.preventDefault();
         
    var dt = e.dataTransfer;
    var files = dt.files;
    
    // THIS SHOULD BE GIVING ME A ONE BUT IT ALWAYS GIVES ME A ZERO INSTEAD
    alert(files.length);
          
    handleFiles(files);
}

.

UPDATE - Fiddle Results

enter image description here

gmustudent
  • 2,229
  • 6
  • 31
  • 43

1 Answers1

1

UPDATE
The actual problem turned out to be that if you try to drag images directly from one web browser tab to this web based drag and drop interface, the event will fire but no files will be dropped. The asker noted this issue on OSX and I was able to replicate the same behavior in Windows 7.


Without seeing your HTML, it's hard to tell what you were having difficulty with. If the ondragover/ondragenter piece wasn't set up correctly then dropping won't work, but you wouldn't see an alert at all, you'd just see the browser render the image from the local filesystem. That also means that you're almost certainly successfully adding the drop event to the correct element.

Try this Fiddle and see if it works for you: http://jsfiddle.net/qey9G/4/

HTML

<div>
        <div id="dropzone" style="margin:30px; width:500px; height:300px; 
                                  border:1px dotted grey;">
               Drag & drop your file here...
        </div>
</div>

JavaScript

var dropzone = document.getElementById("dropzone");

dropzone.ondragover = dropzone.ondragenter = function(event) {
    event.stopPropagation();
    event.preventDefault();
}

dropzone.ondrop= function drop(e) 
{
    e.stopPropagation();
    e.preventDefault();

    var dt = e.dataTransfer;
    var files = dt.files;

    alert(files.length);
}
lmortenson
  • 1,610
  • 11
  • 11
  • Using `ondragover` and `ondrop` rather than `addEventListener` might make it work on IE8 but otherwise (i.e. for modern browsers) this should work just as well with `addEventListener`. In any event, +1 for the simple test. – Matt Browne Mar 06 '13 at 04:48
  • I agree, and overall the way events are being attached here is a little ancient. I'm assuming since he's seeing an alert though that he's successfully attaching all of them. – lmortenson Mar 06 '13 at 04:49
  • The fiddle is saying 0 for me in Chrome and in Mozilla – gmustudent Mar 06 '13 at 04:51
  • Hmm... I'm getting a 1 in Chrome/Mozilla and I assume at least @MattB. did as well, so it may not be related to the code. That leaves the OS, the file you're dropping, and how you're dropping the file. Can you describe all of those things? – lmortenson Mar 06 '13 at 04:53
  • I posted an image within my post for you. – gmustudent Mar 06 '13 at 04:53
  • That looks like a Mac background, maybe it has something to do with that? I'm successfully able to do it on Windows 7 Chrome and Firefox. – lmortenson Mar 06 '13 at 04:54
  • I use a mac, and I am taking the file from another tab, going into the fiddle tab and dropping it. Very standard nothing fancy. the image is just a jpeg. Should I just not do this and stick to regular file uploads since it doesen't work everywhere? – gmustudent Mar 06 '13 at 04:55
  • 1
    I'm not sure if that will work...Macs have special handling to allow you to drag and drop an image -- to *download* it -- from a web page to the Finder, or your desktop, which may be interfering. Try uploading the image directly from Finder. – Matt Browne Mar 06 '13 at 04:56
  • In the post you linked what does he mean with the mime type and his solution to the problem? – gmustudent Mar 06 '13 at 04:58
  • I think that's got to be it, try dropping from Finder. – lmortenson Mar 06 '13 at 04:58
  • @gmustudent I deleted because I realized it probably wasn't relevant – lmortenson Mar 06 '13 at 04:59
  • And uploading from my finder works. That's really not cool that I can only do it that way b/c tons of ppl want to do uploads from the web! But your awesome for realizing it thank you! – gmustudent Mar 06 '13 at 04:59
  • 1
    Np...I guess you'll just have to explain to users that they have to drag and drop the image to Finder (or their desktop) first, then from Finder to your page. – Matt Browne Mar 06 '13 at 05:01
  • 1
    Thank you all for your assistance! I need to get my self back over to Java where things make sense :) – gmustudent Mar 06 '13 at 05:04
  • Haha...there seems to be a promising answer or two here: http://stackoverflow.com/questions/3694631/html5-drag-and-drop-between-windows. But that does require making things a bit more complicated... – Matt Browne Mar 06 '13 at 05:04
  • This guy figured out how to get cross browsers to get the image url. any idea if it could be tweaked for the purposes of image uploads? – gmustudent Mar 06 '13 at 05:11
  • I think what I'll do is check if the count is equal to zero. If it is that means it came from a different browser so I will grab the image url instead and then upload it by taking the image from the url source. and if both are null then this user can find a new site b/c idk what's up with them! thanks again for the direction – gmustudent Mar 06 '13 at 05:30