I recently encountered a problem when trying to implement drag and drop file uploads in a site I’m working on that uses Knockout.js. A simplified reproduction of the problem is as follows:
The problem that I encountered was that when I attempted to retrieve the files from the event object (line 15), e.dataTransfer was undefined.
The problem ended up being that when using Knockout with jQuery, the event object sent to the view model function is a jQuery event object. While jQuery event objects are helpful in as much as they contain additional information not found in standard event objects, they do not expose certain case-specific event members such as dataTransfer. In order to get to dataTransfer, we need to access it via the jQuery event’s “originalEvent” property like so.
This returns the original event object, which contains the dataTransfer property that will allow us to get to our files.
Note: If you aren’t using jQuery Knockout sends up standard event objects so you won’t encounter this problem. That said, if you’re using Knockout chances are you’re probably using jQuery as well.