In a previous post, we showed you how to upload several files using the input element. In Firefox 3.6, you can let your users drag and drop files directly into your web page, without going through the file picker.
Demo
If you’re running the latest Firefox 3.6 beta, check out our interactive demo of drag and drop. It showcases two technologies: the Drag and Drop API and the File API.
Drag and Drop Events
To use drag and drop, you first need to tell the browser that a given element can handle dropped objects and will respond to a drop action, using the dragover and drop events.
You also need to prevent the browser’s default behavior, which is to simply load the dropped object in the browser window.
dropzone.addEventListener("dragover", function(event) { event.preventDefault(); }, true); dropzone.addEventListener("drop", function(event) { event.preventDefault(); // Ready to do something with the dropped object }, true); |
You may also want to use the dragenter and dragleave events to be notified when a drag session starts or stops.
Your element is now ready to receive files with the drop event.
Manipulating the Files
On the drop event, you can access the files with the files property of the DataTransfer object:
dropzone.addEventListener("drop", function(event) { event.preventDefault(); // Ready to do something with the dropped object var allTheFiles = event.dataTransfer.files; alert("You've just dropped " + allTheFiles.length + " files"); }, true); |
Once you’ve accessed the files, the File API lets you do much more, like parsing files as a binary array, or displaying a preview of an image by reading the file as a DataURL.
Of course, you can still drag and drop data other than files (e.g. text, URLs, remote images …) using the drag and drop API.
Pingback from interactive file uploads with Drag and Drop, FileAPI and XMLHttpRequest ✩ hacks.mozilla.org on December 28th, 2009 at 5:51 pm:
Pingback from Extract all urls from the last firefox sessionstore.js file used. - Amit Agarwal on January 5th, 2010 at 8:42 am:
Pingback from Potegni in spusti v Firefox 3.6 » Kratkočasnik on January 13th, 2010 at 4:22 am:
Pingback from Webbased 3D viewer | No Pity for the Masses on January 24th, 2010 at 7:57 pm:
Pingback from firefox 3.6 « kravca.mu/blog on January 26th, 2010 at 4:30 am:
Pingback from Firefox: 46 features you might not know about ✩ Mozilla Hacks – the Web developer blog on February 24th, 2010 at 6:19 am:
Pingback from 谋智社区 » Blog Archives » Firefox:您可能还不知道的46项功能 on March 5th, 2010 at 2:14 am:
Pingback from an HTML5 offline image editor and uploader application ✩ Mozilla 웹 기술 블로그 on July 7th, 2010 at 9:04 am:
Pingback from file drag and drop in Firefox 3.6 ✩ Mozilla 웹 기술 블로그 on July 7th, 2010 at 9:21 am:
Pingback from interactive file uploads with Drag and Drop, FileAPI and XMLHttpRequest ✩ Mozilla Hacks – the Web developer blog on April 13th, 2012 at 4:20 am: