I will advise you prior that I have limited JavaScript experience.
Currently, I have the JavaScript code:
$('#xhr-filebox').bind({
"dragover": HandleDragEvent,
"dragleave": HandleDragEvent,
"drop": HandleDropEvent
});
function HandleDropEvent(e){
var files = e.target.files || e.dataTransfer.files;
UploadFile(files[0]);
}
(some code is omitted, but I will add more if you request)
…and the HTML:
<div class="filebox" id="xhr-filebox">
<p id="xhr-action">...or click me to pick one.</p>
</div>
However, when I drag a file into it, the Chrome JS console says this:
Uncaught TypeError: Cannot read property ‘files’ of undefined
It can however get the FileList object when reading from a file input.
Curiously, when I log the event argument ( console.log(e) ), it logs it as f.event, whereas in a similar script of mine, it logs it as MouseEvent (screenshot: https://i.stack.imgur.com/3krcT.png)
Unlike the bind() function in jQuery, this uses the addEventListener() function of a DOM object returned by getElementById(), IE this is pure JavaScript. I have tried that method but nothing new happens.
If
filesdoesn’t exist one.target, you’re looking for afilesone.dataTransfer— but if there’s nodataTransferproperty one(and there doesn’t seem to be in your screenshot) you’ll try to dereferenceundefined, resulting in this error.I’d change the code to:
That way, if
e.target.filesdoesn’t exist ande.dataTransferalso doesn’t exist, you’ll getfilesbeingundefined, rather than an error.The event object that jQuery gives you is created and normalized by jQuery. Since we know
e.dataTransferdoesn’t exist on that event object (from your screenshot), my guess is that it’s not one of the properties jQuery copies over from the original event object. That’s okay, though, because jQuery gives us access to the original event viae.originalEvent. So I’d try:That grabs
filesfrome.targetif that’s where it is, or from thedataTransferobject wherever we find it (one, or one.originalEvent).