Drag & Drop - Datatransfer Object
Solution 1:
The drag data store has different modes depending on when you access it:
- On
dragstartevent it's on read/write mode. - On
dropevent, it's in read only mode. And on all other events, it's in protected mode.
Protected mode is defined this way:
Protected mode
For all other events. The formats and kinds in the drag data store list of items representing dragged data can be enumerated, but the data itself is unavailable and no new data can be added.
See here: https://html.spec.whatwg.org/multipage/interaction.html#the-drag-data-store
That means that when you access the dataTransfer object in your console, which is not on drop or dragstart event, it's in protected mode, preventing you from accessing the data.
You can view the fileList because you log the fileList on drop event when dataTransfer is readable. But if you log e.dataTransfer or e, you won't be able to access any data.
You can test here, even on dragover you can't access what's in dataTransfer:
document.querySelector('#droppable').ondragover = function(e) {
console.log('on dragover files are', e.dataTransfer.files)
e.preventDefault();
}
document.querySelector('#droppable').ondrop = function(e) {
console.log('on drop files are', e.dataTransfer.files)
e.preventDefault();
}<divid=droppable>Drop a file</div>
Post a Comment for "Drag & Drop - Datatransfer Object"