Example html5 api file with jquery?

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.

+21
jquery file api html5
Jan 18 2018-11-11T00
source share
2 answers

The solution is simple.

  • Lose the on prefix (which did not cause any events)
  • this. => $(this). (therefore, when events did not occur, this gave an error).

It worked for me.

+21
Jan 18 2018-11-11T00:
source share

Description Gidon solves the problem. Here is a fully coded example if someone else wants to solve this problem and wants more details.

 // Bindings to HTML; replace these with your components. var $dropArea = $('#dropArea'); var $picsHolder = $('#picsHolder'); // Attach our drag and drop handlers. $dropArea.bind({ dragover: function() { $(this).addClass('hover'); return false; }, dragend: function() { $(this).removeClass('hover'); return false; }, drop: function(e) { e = e || window.event; e.preventDefault(); // jQuery wraps the originalEvent, so we try to detect that here... e = e.originalEvent || e; // Using e.files with fallback because e.dataTransfer is immutable and can't be overridden in Polyfills (http://sandbox.knarly.com/js/dropfiles/). var files = (e.files || e.dataTransfer.files); var $img = $('<img src="" class="uploadPic" title="" alt="" />'); for (var i = 0; i < files.length; i++) { (function(i) { // Loop through our files with a closure so each of our FileReader are isolated. var reader = new FileReader(); reader.onload = function(event) { var newImg = $img.clone().attr({ src: event.target.result, title: (files[i].name), alt: (files[i].name) }); // Resize large images... if (newImg.size() > 480) { newImg.width(480); } $picsHolder.append(newImg); }; reader.readAsDataURL(files[i]); })(i); } return false; } }); 
 #dropArea { border: 10px dashed; border-radius: 10px; border-color: gray; width: 200px; height: 200px; } #dropArea:hover { border-color: black; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="picsHolder"></div> <div id="dropArea"></div> 
+32
Jun 25 '11 at 20:26
source share



All Articles