e.dataTransfer.files.length showing up as 0
Asked Answered
S

2

7

I am doing a pretty standard drag and drop feature in HTML5. However, for some reason e.target.dataTransfer showing up as undefined. I am new to javascript. Any help will be appreciated.

This is the java script.

    <script src="jquery-2.1.4.min.js"></script>
<script>
    $(document)
            .ready(
                    function() {

                        jQuery.event.props.push('dataTransfer');

                        if (!!window.FileReader) {
                            // Browser suppports FILE API.  

                            // is XHR2 available?
                            var xhr = new XMLHttpRequest();
                            if (xhr.upload) {
                                $('#drag-and-drop-zone').bind('dragover',
                                        function(e) {
                                            e.preventDefault();
                                            e.stopPropagation();
                                            $(this).addClass('drag-over');
                                            return false;
                                        });
                                $('#drag-and-drop-zone').bind('dragleave',
                                        function(e) {
                                            e.preventDefault();
                                            e.stopPropagation();
                                            $(this).removeClass('drag-over');
                                            return false;
                                        });

                                $('#drag-and-drop-zone')
                                        .bind(
                                                'drop',
                                                function(e) {

                                                    e.preventDefault();
                                                    e.stopPropagation();

                                                    $(this).removeClass(
                                                            'drag-over');
                                                    alert(this.id);
                                                    alert('This shows up as 0. Why? . '
                                                            + e.dataTransfer.files.length);

                                                    return false;
                                                });

                            }

                        } else {

                            // Browser does not support FILE API. 
                            $('#drag-and-drop-zone')
                                    .html(
                                            'Upgrade your browser. Find something that can handle HTML5.');
                        }

                    });
</script>

And this is the HTML

    <body>
    <div id="drag-and-drop-zone">Drag and drop the organization data
        file on this area.</div>

</body>

When I run this - somehow the file drop is registered, as in the correct events are triggered. But somehow the number of files show up as 0. Please help.

Symptomatology answered 26/7, 2015 at 18:24 Comment(0)
B
3

Very interesting!

Add the following lines to the drop function you have - in front of your alert messages.

var files = e.dataTransfer.files;
for (var i = 0, f; f = files[i]; i++) {
    console.log('f: ', f);
}

You'll notice your alert no longer says 0. When I dragged a single file, it showed 1 (as expected). It also showed the proper count when I dragged multiple files.

I'm no expert of the internals, but when I looked at the FireBug console, I noticed the FileList was not presented as a regular type of object. I suspect that the File API relies on "Just In Time" delivery of content. It makes sense, why carry around the overhead of all the data when the drop container may not properly handle it.

Once the data is requested (with the for loop), the File API populates the appropriate internal data structures for use by the calling code.

Notice that the for loop doesn't loop through a count - it actually requests a file at the given index. The File API returns false if the file is not available. That is what actually causes the file to get loaded for use by your code - as well as cancel the for loop when there are no more files.

While I can't specifically answer your question of "why", at least this can get you moving along. Once you've looped through the available files, you can make use of the FileList length (and any other data that you need).

I hope this helps.

Good Luck!

Brunabrunch answered 26/7, 2015 at 20:53 Comment(0)
S
0

Ok. Got it. The issue was with the version of Firefox I was using. I tried with IE11 and it is working fine now. I have failed to check for the support of FileApi.

Symptomatology answered 27/7, 2015 at 5:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.