How To Get Filename On Html Javascript Jquery Drag/drop Event (non Html5)
Solution 1:
I've found plenty on how to do this in html5, and after searching through the event variable in the browser debugger, i was able to find exactly what i was looking for.
I just have to say this was far more simple than i thought it would have been considering i spent at least an hour looking for a solution on the net.
What you have to do is get the "originalEvent" from the jquery event, which will have a dataTransfer attribute. The dataTransfer attribute contains an array of files, which you can iterate though (if they exist), and get the name attribute of each file (along with lastModified, lastModifiedDate, size, and type.
the code needed to get the name of the first file from the drop was: e.originalEvent.dataTransfer.files[0].name
you can get the length of the file array by:
e.originalEvent.dataTransfer.files.length
so just an example, to iterate through the files that were dragged and dropped onto the dive, you could do:
for (var i = 0; i < e.originalEvent.dataTransfer.files.length; i++) {
alert(e.originalEvent.dataTransfer.files[i].name);
}
To get the "drop" event to fire, i needed to "cancel out" the dragover, dragenter, and dragleave events of the div (dragover may not have to be canceled out?)
heres my solution:
html code:
<div id="dropimagebox" class="noselect">Drop image here or click</div>
javascript code:
functioninitDragAndDropDialog()
{
document.getElementById("imagedialog").showModal();
$("#dialoginnerds").bind('clickoutside',function(){
document.getElementById("imagedialog").close();
});
$("#dialoginnerds").on("dragover", function(e) {
e.preventDefault();
e.stopPropagation();
$("#dropimagebox").addClass('dragging');
});
$('#dialoginnerds').on('dragenter',function(e){
e.preventDefault();
e.stopPropagation();
$("#dropimagebox").addClass('dragging');
})
$('#dialoginnerds').on('dragleave',function(e){
e.preventDefault();
e.stopPropagation();
$("#dropimagebox").removeClass('dragging');
})
$("#dialoginnerds").on('drop', function (e) {
e.preventDefault();
alert(e.originalEvent.dataTransfer.files[0].name);
});
}
Post a Comment for "How To Get Filename On Html Javascript Jquery Drag/drop Event (non Html5)"