Uploader OverviewThe Fluid Uploader is designed to get around the rather limited functionality and poor user feedback provided by the standard HTML file upload form element. In order to allow the selection of multiple files, and to provide a more consistent and responsive interface, the Uploader uses a browser-based plugin. The current Uploader is implemented with a Flash helper object, the same technique used by Flickr and the YUI library. The Flash-based Uploader has the advantage of the near-ubiquity of Flash on browsers, but must work-around some limitations in the Flash platform, particularly accessibility. Other Upload helpers are being explored, such as one implemented with Google Gears, which would provide more flexibility. NOTE: If you will be testing the Uploader on a local file system, please see the Important Notes for Developers below.
For more details on the Uploader design and design Process, see Uploader Design Overview. Progressive EnhancementThe Uploader also utilizes the concept of progressive enhancement. The goal is to ensure that the page is usable by the widest possible audience, even with old browsers or when JavaScript is turned off. This is done by specifying a regular file input element in the markup. When the Uploader is initialized, the Upload code will replace that element with the Fluid Uploader. Progressive enhancement is available by calling the fluid.progressiveEnhanceableUploader() creator function. Upload ManagerThe Fluid Uploader, like many Fluid components, is really one interface to a collection of components that work together to provide a unified user experience. At the heart of the Uploader is an uploadManager. Two different uploadManagers are provided as of the Infusion 1.0 Uploader:
In the future other Upload Managers may be provided. You can specify a different Upload Manager by setting the uploadManger option when you instantiate the uploader. The default is the fluid.swfUploadManager. UpgradingUpgrading from Infusion 0.8: The classnames used for selectors and styles in the Uploader were extensively refactored for Infusion 1.0. Please refer to the Tutorial - Uploader 1.0 Migration documentation before upgrading from Infusion 0.8. Upgrading from Infusion 0.5: During Infusion 0.6, the Uploader API had a complete overhaul. Please refer to this document and the latest example code before upgrading from Infusion 0.5 or earlier. |
Status This component is in Preview status On This Page See Also Still need help? Join the infusion-users mailing list and ask your questions there. |
Creating an Uploader
To instantiate a new Uploader on your page using the recommended progressive enhancement feature:
var myUploader = fluid.progressiveEnhanceableUploader(container, enhanceable, options);
Returns: The Uploader component object.
Parameters
container
The container is a CSS-based selector, single-element jQuery object, or DOM element that identifies the root DOM node of the Uploader markup.
enhanceable
The enhanceable is a CSS-based selector, single-element jQuery object, or DOM element indicating the DOM element to show if the system requirements aren't met.
options
The options object is an optional data structure that configures the Uploader, as described in the Options section below.
Supported Events
The Uploader fires the following events (for more information about events in the Fluid Framework, see Events for Component Users):
| Event | Type | Description | Parameters | Parameter Description |
|---|---|---|---|---|
| afterReady | default | This event is fired when the Flash (swf) object is instantiated and ready. |
none | |
| onFileDialog | default | The event is fired when the OS File Open dialog is displayed. |
none | |
| afterFileQueued | default | This event is fired for each file that is queued after the File Selection Dialog window is closed. | file | file: the File object that has just been added to the queue. |
| afterFileRemoved | default | Event is fired for each file that is removed from the filequeue model |
file | fie: the File object that was just removed from the queue |
| onQueueError | default | This event is fired for each file that was not queued after the File Selection Dialog window is closed. | file, errorCode | file: the File object that failed to be added to the queue. errorCode [number]: indication of the reason for the file not being added to the queue. Use that.swfUploader.QUEUE_ERROR to interpret the value. Note: A file may not be queued for several reasons such as, the file exceeds the file size, the file is empty or a file or queue limit has been exceeded. |
| afterFileDialog | default | This event fires after the File Selection Dialog window has been closed and all the selected files have been processed. | none | |
| onUploadStart | default | This event at the beginning of the entire upload cycle. | fileList | fileList: a list of the File objects that will be uploaded to the server. |
| onFileStart | default | This event is fired immediately before the file is uploaded. | file | file: the File object that has started to be uploaded. |
| onFileProgress | default | This event is fired periodically by the upload manager during a file upload and is useful for providing UI updates on the page. Also keeps up-to-date the overall progress of the currently uploading batch. |
file, fileBytesComplete, fileTotalBytes | file: the File Ooject for this progress event fileBytesComplete [number]: the number of bytes for the current file that have been uploaded to the server fileTotalBytes [number]: the total size of the file in bytes. |
| onFileError | default | This event is fired any time an individual file upload is interrupted or does not complete successfully. |
file, errorCode, message | file: the File object that errored. errorCode [number]: whether the file failed because the user cancelled the upload or because of another file specific error Use fluid.uploader.fileStatusConstants to interpret the value. message [string]: a description of the error. |
| onFileSuccess | default | This event is fired when an upload completes and the server returns a HTTP 200 status code. |
file | file: the File object that has completed. |
| afterFileComplete | default | This event is fired for each file uploaded whether or not the file is uploaded successfully (onFileSuccess) or the file failed to upload (onFileError). At this point the upload process for the currently uploading file is complete and the upload for the next file in the queue is started. | file | file: the File object that was successfully uploaded. |
| afterUploadComplete | default | This event is fired at the end of an upload cycle when all the files have either been uploaded, failed to upload, the user stopped the upload cycle, or there was an unrecoverable error in the upload process and the upload cycle was stopped. | fileList | fileList: the list of File objects that "completed" (either succeeded or failed), in this upload. |
Please note that there are other events that are fired by the SWFUpload object which are not used by the Fluid Uploader but that you could add a listener for.
File Objects
Many of the Uploader's events pass a File object as a parameter to the event listener. These objects provide useful information about the file, including its name, size in bytes, and its current status in the queue. Due to limitations with the Flash back-end used by Uploader, these objects should be treated as transient: as different events fire for a single file, you'll get passed a new File object each time.
File object: a representation of each file in the file queue, as provided from the upload manager.
id : string, // a unique id for each file in the queue
name : string, // The file name. The path is not provided.
size : number, // The file size in bytes
type : string, // The file type as reported by the client operating system
creationdate : Date, // The date the file was created
modificationdate : Date, // The date the file was last modified
filestatus : number // The file's current status.
// Use fluid.uploader.fileStatusConstants to interpret the value.
Notes:
- depending on the implementation of the upload manager other values maybe present in the file object.
- the file object should be treated as read-only since it is frequently refreshed by the upload manager during the Uploader workflow.
File Status Constants
The Uploader offers a set of constants used to denote the status of a particular file in the queue. These can be used when querying the filestatus property of a File object. Many of these constants are provided by the underlying SWFUpload library used by the Uploader, and their meaning is not specific to the Uploader.
| Upload Error Constants | Description |
|---|---|
| fluid.uploader.errorConstants.HTTP_ERROR | An HTTP error occurred while uploading a file. |
| fluid.uploader.errorConstants.MISSING_UPLOAD_URL | The uploadURL was not correctly specified in the uploadManager options. |
| fluid.uploader.errorConstants.IO_ERROR | An IO error occurred while transferring the file. |
| fluid.uploader.errorConstants.SECURITY_ERROR | The upload caused a security error to occur. |
| fluid.uploader.errorConstants.UPLOAD_LIMIT_EXCEEDED | The user attempted to upload more files than allowed by the fileUploadLimit}}option for the {{uploadManager. |
| fluid.uploader.errorConstants.UPLOAD_FAILED | The Uploader was unable to start uploading the file to the server |
| fluid.uploader.errorConstants.SPECIFIED_FILE_ID_NOT_FOUND | This indicates an error in the Uploader and should be filed as a bug |
| fluid.uploader.errorConstants.FILE_VALIDATION_FAILED | |
| fluid.uploader.errorConstants.FILE_CANCELLED | The upload was canceled by the user. |
| fluid.uploader.errorConstants.UPLOAD_STOPPED | The upload was stopped by the user. |
| File Status Constants | |
| fluid.uploader.fileStatusConstants.QUEUED | The file is currently queued up and ready to be sent to the server. |
| fluid.uploader.fileStatusConstants.IN_PROGRESS | The file is currently being uploaded to the server. |
| fluid.uploader.fileStatusConstants.ERROR | An error occurred while trying to upload the file. |
| fluid.uploader.fileStatusConstants.COMPLETE | The file was successfully uploaded to the server. |
| fluid.uploader.fileStatusConstants.CANCELLED | The file was canceled by the user while in the process of being uploaded. |
Options
The Uploader supports a plug-and-play architecture that allows for many of the sub-components of the Uploader to be swapped out for other components or your own custom components. The best example of this is the uploadManager option, which allows you to choose between the fluid.swfUploadManager for use with a server, or the fluid.demoUploadManager for use locally for demos and UI development. However you could also replace the Progress subcomponent, or the File Queue subcomponent, with customized version you have implemented yourself.
The Uploader and its sub-components are also highly configurable; you can make many adjustments to the user experience through a combination of HTML, CSS and the built-in configuration options. To customize the component for your own needs, start with these out-of-the-box features. If you need more flexibility, feel free to to write your own sub-component.
In addition to the Uploader options, there are also options specifically for the File Queue, Progress, and swfUploadManager subcomponents. The options for swfUploadManager contain many of the settings that control which files can be uploaded to the server.
Uploader Options:
| Name | Description | Values | Default |
|---|---|---|---|
| uploadManager | "fluid.swfUploadManager" or "fluid.demoUploadManager" |
"fluid.swfUploadManager" | |
| decorators | Javascript object containing decorators used to setup the DOM correctly for SWFUpload and configure the Uploader component according to the version of Flash and browser currently running. | "fluid.swfUploadSetupDecorator" Note: You would only change this option if you were providing an alternate upload and browsing sub-component than SWFUpload. |
decorators: {
type: "fluid.swfUploadSetupDecorator"
},
|
| fileQueueView | Specifys which fileQueueView sub-component to use. Currently there is only one fileQueueView sub-component. |
"fluid.fileQueueView" |
fileQueueView: {
type: "fluid.fileQueueView"
},
|
| totalProgressBar | Specifys the sub-component and options to use for the total progress bar. | See the Progress API documentation for a full descriptions of the available options. |
totalProgressBar: {
type: "fluid.progress",
options: {
selectors: {
progressBar: ".flc-uploader-queue-footer",
displayElement: ".flc-uploader-total-progress",
label: ".flc-uploader-total-progress-text",
indicator: ".flc-uploader-total-progress",
ariaElement: ".flc-uploader-total-progress"
}
}
},
|
| selectors | Javascript object containing selectors for various fragments of the uploader markup | see Selectors below | |
| listeners | JavaScript object containing events and the listeners that are attached to them. | Keys in the object are event names, values are functions or arrays of functions. | See Supported Events |
| focusWithEvent | Javascript object containing selectors for markup elements that should obtain focus after certain events. | Keys in the object are supported event names. Note: only specific methods in the Uploader have been factored to use this values. |
focusWithEvent: {
afterFileDialog: "uploadButton",
afterUploadStart: "pauseButton",
afterUploadStop: "uploadButton"
},
|
| styles | Specific class names used to achieve the look and feel of the different states of the Uploader |
styles: {
disabled: "fl-uploader-disabled",
hidden: "fl-uploader-hidden",
dim: "fl-uploader-dim",
totalProgress: "fl-uploader-total-progress-okay",
totalProgressError: "fl-uploader-total-progress-errored"
}
|
|
| strings | Strings that are used in the Uploader. Note: The strings that contain tokens (example: %curFileN) are passed through a string template parser. |
|
strings: {
progress: {
toUploadLabel: "To upload: %fileCount %fileLabel (%totalBytes)",
totalProgressLabel: "Uploading: %curFileN of %totalFilesN %fileLabel (%currBytes of %totalBytes)",
completedLabel: "Uploaded: %curFileN of %totalFilesN %fileLabel (%totalCurrBytes)%errorString",
numberOfErrors: ", %errorsN %errorLabel",
singleFile: "file",
pluralFiles: "files",
singleError: "error",
pluralErrors: "errors"
},
buttons: {
browse: "Browse Files",
addMore: "Add More",
stopUpload: "Stop Upload",
cancelRemaning: "Cancel remaining Uploads",
resumeUpload: "Resume Upload"
}
}
|
| demo | Boolean indicating whether to run in "demo" mode. See Running locally: "demo" mode below. |
false | demo: false |
swfUploadManager Options
| Name | Description | Values | Default |
|---|---|---|---|
| uploadURL | The URL to which files should be sent via POST requests. | String |
"" |
| flashURL | The URL to the SWFUpload Flash file, required for the Uploader to function. | String | "../../flash/swfupload.swf" |
| flashButtonPeerId | The id of the element which will be replaced by the Flash movie when using Flash 10. Ordinarily, this is configured by the swfUploadSetupDecorator and needn't be specified. | String |
"" |
| postParams | Parameters to send along with the POST request to the server when uploading files. | Javascript Object |
{} |
| fileSizeLimit | The maximum size of a file to send to the server. Files larger than this will not be added to the queue. May be specified with a unit. For example: "2147483648 B", 2097152, "2097152KB", "2048 MB", "2 GB" | String |
"20480" |
| fileTypes | The type of files that are allowed to be uploaded. Each file type should be specified as *.[file extension], separated by semicolons. Example: "*.jpg;*.jpeg;*.gif;*.tiff" | String |
"*.*" |
| fileTypesDescription | A human-readable description of the types of files allowed. This is displayed in the Browse Files dialog. | String |
null |
| fileUploadLimit | The maximum number of files allowed to be uploaded. 0 is unlimited. | Integer | 0 |
| fileQueueLimit | The number of files that can be queued at once before uploading them. 0 is unlimited. | Integer |
0 |
| debug | Shows the SWFUpload debug window when set to true. | boolean | false |
demoUploadManager Options
| Name | Description | Values | Default |
|---|---|---|---|
| simulateDelay | Used to add a random delay between progress events to more accurately simulate server interaction. The delay however gets in the way of automated testing and so can be set to false. |
boolean | true |
swfUploadSetupDecorator Options
The fluid.swfUploadSetupDecorator is a small subcomponent designed to set up the DOM correctly for use by the SWFUpload library we use to power the Uploader. By default, it is turned on in the Uploader component, and you shouldn't need to turn it off. The swfUploadDecorator provides a few options that may be of interest to you in customizing the appearance of the Uploader when using it with Flash 10:
| Name | Description | Values | Default |
|---|---|---|---|
| flashButtonAlwaysVisible | Specifies if a visible, Flash-based Browse Files button should always appear, even in Flash 9 | boolean | false |
| flashButtonImageURL | Path to the Browse Files button image. This image should include each of the button states, stacked on top of each other. Take a look at the default browse.png for an example | String | "../../images/uploader/browse.png" |
| flashButtonHeight | The height of the Browse Files button, in pixels. | Number | 22 |
| flashButtonWidth | The width of the Browse Files button, in pixels | Number | 106 |
| styles | CSS class names to use when styling the Browse Files overlay button. | Object |
styles: {
browseButtonOverlay: "fl-browse-button-overlay"
}
|
Selectors
One of the options that can be provided to the Uploader is a set of CSS-based selectors identifying where in the DOM different elements can be found. The value for the option is itself a Javascript object containing name/value pairs:
selectors: {
selector1Name: "selector 1 string",
selector2Name: "selector 2 string",
...
}
The different parts of the Uploader interface each have their own set of selectors (though all selectors must be provided together in a single object). Each also has a default, as defined below:
General
| Selector name | Description | Default |
|---|---|---|
| fileQueue | The container element of the File Queue. | ".flc-uploader-queue" |
| browseButton | The Browse Files button. | ".flc-uploader-button-browse" |
| uploadButton | The Upload button. | ".flc-uploader-button-upload" |
| pauseButton | The Pause button. | ".flc-uploader-button-pause" |
| totalFileProgressBar | The file container for the total progress bar. | ".flc-uploader-queue-footer" |
| totalFileStatusText | The element to write the total progress bar status text into. | ".flc-uploader-total-progress-text" |
| instructions | The element containing the browse files instructions. | ".flc-uploader-browse-instructions" |
File Queue
| Selector name | Description | Default |
|---|---|---|
| fileRows | The files rows in the queue. | ".flc-uploader-file" |
| fileName | The container for the file's name. Scoped within an individual file row. | ".flc-uploader-file-name" |
| fileSize | The container for the file's size. Scoped within an individual file row. | ".flc-uploader-file-size" |
| fileIconBtn | The container for the file row icons. Scoped within an individual file row. | ".flc-uploader-file-action" |
| errorText | The container for file specific error text. Scoped within an individual file row. | ".flc-uploader-file-error" |
| rowTemplate | A template element to clone when creating new rows in the file queue. | ".flc-uploader-file-tmplt" |
| errorInfoRowTemplate | A template element to close when displaying an error for an individual file. | ".flc-uploader-file-error-tmplt" |
| rowProgressorTemplate | A template element to clone when creating the progress bar for a file row. | ".flc-uploader-file-progressor-tmplt" |
Scroller
| Selector name | Description | Default |
|---|---|---|
| wrapper | A wrapper container around the scrollable element. | ".flc-scroller" |
Progress
Note: Please see the Progress API document for a full description of Fluid Progress. Uploader uses the following selector options for Progress:
selectors: {
progressBar: ".flc-uploader-queue-footer",
displayElement: ".flc-uploader-total-progress",
label: ".flc-uploader-total-progress-text",
indicator: ".flc-uploader-total-progress",
ariaElement: ".flc-uploader-total-progress"
}
Any selectors not provided as an option will revert to the default. Implementers may choose to use the default class names in their markup, or customize the selectors, or a combination of these two approaches.
For example, if your markup uses all of the default selectors, except for the file queue selector and the remove button selector, you would provide the following selectors option:
selectors: {
fileQueue: "#my-file-queue",
removeButton: "#my-remove-button"
}
Dependencies
Required
The Uploader dependencies can be met by including in the header of the HTML file
- the minified InflusionAll.js file
- the Fluid layout CSS file
- the Uploader CSS file
as shown below:
<link rel="stylesheet" type="text/css" href="/framework/fss/css/fss-layout.css" /> <link rel="stylesheet" type="text/css" href="components/uploader/css/Uploader.css" /> <script type="text/javascript" src="framework/core/js/InfusionAll.js"></script> <script type="text/javascript" src="framework/core/js/ProgressiveEnhancement.js"></script>
NOTE that the file ProgressiveEnhancement.js is not included in InfusionAll.js, and so must be included separately, even if you are using InfusionAll.js.
Alternatively, the individual file requirements are:
<link rel="stylesheet" type="text/css" href="components/uploader/css/Uploader.css"" /> <link rel="stylesheet" type="text/css" href="framework/fss/css/fss-layout.css" /> <script type="text/javascript" src="lib/jquery/core/js/jquery.js"></script> <script type="text/javascript" src="framework/core/js/jquery.keyboard-a11y.js"></script> <script type="text/javascript" src="lib/swfobject/js/swfobject.js"></script> <script type="text/javascript" src="lib/swfupload/js/swfupload.js"></script> <script type="text/javascript" src="framework/core/js/Fluid.js"></script> <script type="text/javascript" src="framework/core/js/ProgressiveEnhancement.js"></script> <script type="text/javascript" src="components/progress/js/Progress.js"></script> <script type="text/javascript" src="components/uploader/js/FileQueue.js"></script> <script type="text/javascript" src="components/uploader/js/SWFUploadManager.js"></script> <script type="text/javascript" src="components/uploader/js/Scroller.js"></script> <script type="text/javascript" src="components/uploader/js/Uploader.js"></script>
Important Notes for Developers
Running locally: "demo" mode
The Upload component requires a server component to accept the uploaded files.
However there are times when you want to run the uploader with out a server: when working on integrating the component with your code, developing or testing the UI, or demonstrating the functionality of the code. For that reason the Uploader has a "demo" mode. In demo mode, the Uploader uses a special version of the uploadManager that pretends to be talking to a server. Most of the code is identical to the server mode because the same events are being fired and the model is exactly the same. Most of the code in the Uploader thinks that there is a server.
To run locally you must specify demo: true in your compontent configuration:
var myUploader = fluid.progressiveEnhanceableUploader(".flc-uploader", ".fl-ProgEnhance-basic", { demo: true, uploadManager: "fluid.swfUploadManager" });
To run locally, it is also necessary to make some minor modifications to your Flash settings in order to allow the Flash component to view files on your local machine from a locally instantiated Flash file. See Enabling Uploader on Local File Systems for instructions.