This fragment is about to be reported (you'll remain on this page): You can enter a comment to clarify the mistake if you would like to: |
Self-contained cross-browser pure JavaScript class for Drag & Drop and AJAX (multi) file upload.
FileDrop is a lightweight JavaScript class for easy-to-use file uploading that works out of the box.
FileDrop uses some ideas and solutions from qq FileUploader (Github) which is similar to FileDrop but almost 3 times as large (although, perhaps, more elaborated).
FileDrop is released in public domain – feel free to use it however you like. I will always appreciate a back link and a comment, though :)
Latest FileDrop release has been on 28th January 2012.
Download FileDrop – includes demo page, minified (filedrop-min.js) and full (filedrop.js) JavaScript files.
Please report problems and suggestions to the comments.
See also on-line demo page and FileDrop project on JSClasses.org.
Note: if you want to use the latest version you can hotlink to it using this URL (it will be backwards compatibile as much as possible):
http://proger.i-forge.net/filedrop-min.js
The following is the minimum required to use FileDrop:
xml<!DOCTYPE html> <html> <head> <title>Basic FileDrop example</title> <script type="text/javascript" src="http://proger.i-forge.net/filedrop-min.js"></script> <style type="text/css"> /* Essential FileDrop element configuration: */ .fd-zone { position: relative; overflow: hidden; width: 15em; text-align: center; } /* Hides <input type="file" /> while simulating "Browse" button: */ .fd-file { opacity: 0; font-size: 118px; position: absolute; right: 0; top: 0; z-index: 1; padding: 0; margin: 0; cursor: pointer; filter: alpha(opacity=0); font-family: sans-serif; } /* Provides visible feedback when use drags a file over the drop zone: */ .fd-zone.over { border-color: maroon; } </style> </head> <body> <!-- A FileDrop area. Can contain any text or elements, or be empty. Can be of any HTML tag too, not necessary fieldset. --> <fieldset id="zone"> <legend>Drop a file inside…</legend> <p>Or click here to <em>Browse</em>..</p> </fieldset> <script type="text/javascript"> // Tell FileDrop we can deal with iframe uploads using this URL: var options = {iframe: {url: 'your-upload-script.php'}}; // Attach FileDrop to an area: var zone = new FileDrop('zone', options); // Do something when a user chooses or drops a file: zone.on.send.push(function (files) { // if browser supports files[] will contain multiple items. for (var i = 0; i < files.length; i++) { files[i].SendTo('your-upload-script.php'); } }); </script> </body> </html>
The above code creates a FileDrop
xml<fieldset> area and sets it up so that it sends each chosen file to a server script (your-upload-script.php). It is self-sufficient and will work in any browser supported by FileDrop.
Download FileDrop | Demo | Page top. Don't forget to give feedback!
An entire FileDrop facility is contained in window.fd. In itself, FileDrop in itself is split into 2 classes: DropHandle and FileDrop, plus one File class. Apart from these there are global options and functions (belonging to window.fd instead of a particular FileDrop class).
FileDrop uses event-driven approach for better flexibility. See also CallOf() and CallAll() functions that call event chains (sets of callbacks).
Global options are set in window.fd:
Download FileDrop | Demo | Page top. Don't forget to give feedback!
This is an abstraction layer that provides FileDrop class with normalized events occurring on file Drag & Drop and Browse. It creates
xml<input type="file" /> and
xml<iframe> elements and handlers their activity.
DropHandle doesn't do anything except handling node events – it doesn't change the appearence of zone or respond to Browse or Drag & Drop, it only invokes corresponding event handlers.
// zone - a DOM element or ID to hook file upload events for. // opt - optional options, see below. new fd.DropHandle(zone, opt);
xml<iframe> fallback settings. Currently has only one field (but FileDrop has more):xml<iframe> element to. Must be a server-side script that handles iframe upload.xml<input type="file" /> element.Note: you can access passed zone element using zone property of the DropHandle class.
Download FileDrop | Demo | Page top. Don't forget to give feedback!
Events are stored in on property, e.g. aDropHandle.on[dragEnter]. Each on member is an array of callbacks.
xml<iframe> upload has finished. response is the object returned by the server script plus several XMLHttpRequiest-like response properties making it suitable to use one callback function both for this and File's done event.xml<iframe> element was constructed and can be set up. Currently there's no default handler.xml<input type="file" /> element was created and needs to be set up. Default handler adds opt.inputClass to it and sets its parent
xml<form>'s style.position to relative to avoid overflow problem in Firefox 10 (it would still show the presumably hidden part of the input even outside the container bounds).xml<input type="file" />.All handlers are of form function (e) where e is the native browser event object. Most useful events are dragEnter and dragLeave because others either don't work, are unstable or unclear.
For a detailed description of HTML 5 Drag & Drop events see this MozDev page.
Only methods of interest are listed here, others are easy to determine from the code.
xml<iframe>, if there was any happening.xml<input type="file"> element is configured for multiple file selection.xml<input type="file">. This affects «Browse» dialog popping up when user clicks on the drop zone (zone node DropHandle was constructed for).xml<iframe> and
xml<form> elements. It will prepare the form and set up the response handler that will call iframeDone event.This class provides the actual FileDrop features. It's based on DropHandle that gives a good degree of cross-browser abstraction. FileDrop uses DropHandle's events to respond to user actions and as such provides the same callbacks as that class.
Also, all DropHandle options, methods and other fields exist in FileDrop as well.
// zone - a DOM element or ID to hook file upload events for. // opt - optional options, see below. new fd.FileDrop(zone, opt); // a global (window[]) alias exists - you should use it instead: new FileDrop(zone, opt);
Options (see also DropHandle options):
xml<iframe> upload will be forced even if browser supports AJAX upload using FileAPI or Chrome/Safari File false by default.Note: you can access DropHandle object by handle property of the FileDrop class.
Download FileDrop | Demo | Page top. Don't forget to give feedback!
Events are stored in on property, e.g. aFileDrop.on[send]. Each on member is an array of callbacks.
xml<iframe> upload can still be done when handling// this event using DropHandle SendViaIFrame(), if desired. Currently there's no default handler.Only methods of interest are listed here, others are easy to determine from the code.
xml<iframe> upload if the browser doesn't support any FileAPI.Browser-independent abstraction layer for dealing with File objects of supported browsers (currenly Firefox/, Chrome and Safari). Wraps around native browser File// object that it's passed in the constructor.
It also provides a number of events wrapping around XMLHttpRequest (see SendTo() method).
// file - a native File object returned by the browser from an event object. new fd.File(file);
File class has the following properties:
Download FileDrop | Demo | Page top. Don't forget to give feedback!
Events are stored in on property, e.g. aFile.on[error]. Each on member is an array of callbacks.
xml<iframe> upload – see SendViaIFrame() with simulated xhr structure.Only methods of interest are listed here, others are easy to determine from the code.
Download FileDrop | Demo | Page top. Don't forget to give feedback!
Comments