I would like to use the html5 api file in combination with the external drag and drop function n (drag the external file to the specified location and capture its contents) and jquery. I found a working example without jquery: ( html5 demo: file api )
var drop = document.getElementById('drop'); drop.ondragover = function () {this.className = 'focus'; return false;}; drop.ondragend = function () { this.className = ''; return false; }; drop.ondrop=function(e) { this.className = ''; e.preventDefault(); var file = e.dataTransfer.files[0]; var reader = new FileReader(); reader.onload = function (evt) { console.log(evt.target.result); } reader.readAsText(file); };
It works great. Now I would like to make this more jquery-ish, and I tried:
$("#drop").bind('ondragover',function() {this.addClass('focus'); return false;}) .bind("ondragend",function () { this.removeClass('focus'); return false;}) .bind("ondrop",function(e) { this.removeClass("focus"); e.preventDefault(); var file = e.dataTransfer.files[0]; var reader = new FileReader(); reader.onload = function (evt) { console.log(evt.target.result); } reader.readAsText(file); });
But this does not work, none of the related events fires. I also tried to lose the "on" part for event names, but this also does not work. Hope someone here can shine the light?
Regards, Jeroen.
jquery file api html5
dr jerry Jan 18 2018-11-11T00 : 00Z
source share