Uncaught TypeError: Cannot set property 'dropEffect' of undefined main.js
Uncaught TypeError: Cannot read property 'files' of undefined
What's wrong here
.coffee
$ ->
app = new Viewr
class Viewr
constructor: () ->
$('#drop_zone')
.bind('dragover', @handleDragOver)
.bind('drop', @handleDrop)
handleDrop: (evt) ->
evt.stopPropagation()
evt.preventDefault()
files = evt.dataTransfer.files
@setActiveImage files[0]
handleDragOver: (evt) ->
evt.stopPropagation()
evt.preventDefault()
evt.dataTransfer.dropEffect = "copy"
setActiveImage: (image)->
$("img").attr "src", src
.Html
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/main.css">
</head>
<body>
<div id="drop_zone" style="width: 100px; height: 100px; border: 2px dashed #bbb;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
border-radius: 5px;
padding: 25px;
text-align: center;
color: #bbb;">Drop files here</div>
</body>
<script type="text/javascript" src="js/lib/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="js/main.js"></script>
</html>
I think the problem may be that you are using the jQuery 'dragover'
and 'drop'
event rather than the native drop event. As you can see here, it mentions having to add the DataTransfer object to the jQuery event object:
OtherProperties
Certain events may have properties specific to them. Those can be accessed as properties of the event.originalEvent object. To make special properties available in all event objects, they can be added to the jQuery.event.props array. This is not recommended, since it adds overhead to every event delivered by jQuery.
Example:
// add the dataTransfer property for use with the native `drop` event // to capture information about files dropped into the browser window jQuery.event.props.push("dataTransfer");
Your best bet is probably to access the originalEvent
object, so see if this works:
handleDrop: (evt) ->
evt.stopPropagation()
evt.preventDefault()
files = evt.originalEvent.dataTransfer.files
handleDragOver: (evt) ->
evt.stopPropagation()
evt.preventDefault()
evt.originalEvent.dataTransfer.dropEffect = "copy"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With