Friday, April 16, 2010

Drag and drop file uploads in Gmail using just the specs

Thursday, April 15th, 2010

Drag and drop file uploads in Gmail using just the specs

Category: Browsers, JavaScript

Gmail started off with the awful input type="file" "add more" typical solution that we all know and love. Then they added the ability to select multiple files via Flash.... and now they allow the ability to drag and drop files right onto the message compose using HTML5 standards.

Want to do it too? Check out the APIs and how you can do it all, including showing the thumbnails:

JAVASCRIPT:
  1. function handleFiles(files) {
  2.   for (var i = 0; i <files.length; i++) {
  3.     var file = files[i];
  4.     var imageType = /image.*/;
  5.     if (!file.type.match(imageType)) {
  6.       continue;
  7.     }
  8.     var img = document.createElement("img");
  9.     img.classList.add("obj");
  10.     img.file = file;
  11.     preview.appendChild(img);
  12.     var reader = new FileReader();
  13.     reader.onload = (function(aImg) { return function(e) { aImg.src = e.target.result; }; })(img);
  14.     reader.readAsDataURL(file);
  15.   }
  16. }

Posted by Dion Almaer at 7:17 pm
5 Comments

++++-
4.5 rating from 17 votes
-->

Posted via web from Rocha's place

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.