# SWFUpload v2.2.0.1 Documentation

## TOC

1. SWFUpload
2. SWFUpload Version 2
3. Overview
4. Getting Started
5. SWFUpload JavaScript Object
   1. Constructor
   2. Globals and Constants
      1. instances
      2. movieCount
      3. QUEUE_ERROR
      4. UPLOAD_ERROR
      5. FILE_STATUS
      6. BUTTON_ACTION
      7. CURSOR
      8. BUTTON_WINDOW_MODE
   3. Properties
      1. customSettings
      2. movieName
   4. Methods
      1. addSetting (deprecated)
      2. getSetting (deprecated)
      3. retrieveSetting (removed in v2.1.0)
      4. destroy (added in v2.1.0)
      5. displayDebugInfo
      6. selectFile
      7. selectFiles
      8. startUpload
      9. cancelUpload
      10. stopUpload
      11. getStats
      12. setStats
      13. getFile
      14. addPostParam
      15. removePostParam
      16. addFileParam
      17. removeFileParam
      18. setUploadURL
      19. setPostParams
      20. setFileTypes
      21. setFileSizeLimit
      22. setFileUploadLimit
      23. setFileQueueLimit
      24. setFilePostName
      25. setUseQueryString
      26. setDebugEnabled
      27. setButtonImageURL (added in v2.2.0)
      28. setButtonDimensions (added in v2.2.0)
      29. setButtonText (added in v2.2.0)
      30. setButtonTextStyle (added in v2.2.0)
      31. setButtonTextPadding (added in v2.2.0)
      32. setButtonDisabled (added in v2.2.0)
      33. setButtonAction (added in v2.2.0)
      34. setButtonCursor (added in v2.2.0)
   5. Events
      1. flashReady
      2. swfUploadLoaded
      3. fileDialogStart
      4. fileQueued
      5. fileQueueError
      6. fileDialogComplete
      7. uploadStart
      8. uploadProgress
      9. uploadError
      10. uploadSuccess
      11. uploadComplete
      12. debug
   6. SWFUpload Utility Objects
      1. Settings Object
      2. Settings Description
      3. File Object
      4. Stats Object
6. SWFUpload Plug-ins
7. Known Issues

## SWFUpload

SWFUpload is a client-side file upload tool originally developed by Vinterwebb.se. It uses a combination of Flash and JavaScript to provide file upload functionality beyond what the basic browser provides with the <input type="file" /> tag.

The main features that SWFUpload provides are:

- The ability to select multiple files in the file browser dialog.
- AJAX-style uploading without a page refresh.
- Upload progress events.
- Namespaced classes compatible with other JavaScript libraries (i.e., jQuery, Prototype, etc.).
- Flash 9 and Flash 10 support. (Flash 8 support dropped in version 2.2.0)

SWFUpload is different from other Flash based upload tools because of the philosophy behind its design. SWFUpload gives developers control by leaving the UI in the browser (as much as possible). Developers can use XHTML, CSS, and JavaScript to tailor the upload UI to the needs and style of their site. Upload status updates are made through a set of simple JavaScript events. The developer uses these events to update the webpage as the file upload progresses.

Unfortunately Flash Player 10 has forced us have one button in a flash movie in order to trigger the file browser dialog window. SWFUpload still empowers the devloper by provding the ability the button and overlay text from JavaScript.

## SWFUpload v2

SWFUpload v2 includes new advanced features, improved stability, Flash Player bug work-arounds and a helpful set of Plug-ins. New features include:

- Flash Player 10 security compatibility.
- The ability to send additional POST values with the uploads.
- Sending per file POST values.
- Complete set of events.
- All settings dynamically updateable.
- Retrieve result data from the server.
- Stop an upload without cancelling.
- Upload files in any order.
- Multi- & Single-file selection dialogs.
- Queue size, files uploaded and file size limits.
- Properly handling of zero-byte files.
- Pre-upload validation event.
- Work-arounds for bugs in Flash and Browser.

## Overview

### HTML Upload

The standard HTML upload input box provides a box and a button to the user for selecting a file. The file is submitted with the form. The entire file must be uploaded before the next page is displayed. No pre-upload validation can be made on the file (e.g., file size limits or valid extensions). Very little feedback is given to the user while the file uploads.

The usage pattern for standard HTML uploads is simple, straight forward, and supported by nearly all browser.

### SWFUpload

SWFUpload uses a Flash movie to handle file selection and upload. A customizable button is displayed by the Flash movie that activates Flashes advanced file selection dialog window. The file selection dialog is configured to allow the user select to a single file or multiple files. The file types can be restricted so users only select the appropriate files (e.g., *.jpg;*.gif).

Once selected each file is validated and queued. As the file is uploaded by Flash several JavaScript events are fired which the developer handles in order to update the page's UI allowing you to provide upload status and error messages in real-time.

The uploaded file is submitted separately from the rest of the page and form. Each file is uploaded individually keeping the server-side upload handling script simple. Since Flash is providing the upload service the page does not have to be submitted or reloaded. The usage pattern for SWFUpload is more like that of an AJAX application than that of a standard HTML form. The page's form will be processed separately from the file upload.

## Getting Started

SWFUpload is not a drag & drop upload control. It requires knowledge of JavaScript and the DOM. Several demos are available that show some of the things SWFUpload is capable of and how to accomplish common tasks.

SWFUpload consists of 4 pieces:

1. Initialization and Settings (JavaScript)
2. JavaScript library: SWFUpload.js
3. Flash Control: swfupload.swf
4. The Event Handlers (JavaScript)

Most problems implementing SWFUpload are caused by incorrect settings, poorly built event handlers, Flash/Browser Bugs, or server configuration.

### Initialization and Settings

SWFUpload must be initialized on the page. This is commonly done in the window.onload event. The SWFUpload constructor takes a settings object. The settings object can be passed directly to the constructor as an object literal.

A reference to the initialized SWFUpload object should be kept as it is needed to start uploads and to control other aspects of SWFUpload.

Example: Initializing SWFUpload with an object literal

`var swfu; window.onload = function () { swfu = new SWFUpload({ upload_url : "http://www.swfupload.org/upload.php", flash_url : "http://www.swfupload.org/swfupload.swf", file_size_limit : "20 MB" }); };`

Example: Initializing SWFUpload with a settings object stored in a variable

`var swfu; window.onload = function () { var settings_object = { upload_url : "http://www.swfupload.org/upload.php", flash_url : "http://www.swfupload.org/swfupload.swf", file_size_limit : "20 MB" }; swfu = new SWFUpload(settings_object); };`

### JavaScript library

The JavaScript library file (swfupload.js) must be included on the page where the user will upload files.

Once a SWFUpload object has been created access to several functions become available which allow the developer to control SWFUpload.

Example: Adding SWFUpload.js to a page

`<script type="text/javascript" src="http://www.swfupload.org/swfupload.js"></script>`

Example: Initializing SWFUpload with the required settings.

`var swfu = new SWFUpload({ upload_url : "http://www.swfupload.org/upload.php", flash_url : "http://www.swfupload.org/swfupload_f9.swf", button_placeholder_id : "spanSWFUploadButton" });`

### Flash Control

The SWFUpload JavaScript library dynamically loads the Flash Control (swfupload.swf).

The location of the Flash Control file must be provided in the SWFUpload settings object during setup.

The Flash Control is a small Flash Movie that handles file browsing, validation and upload. It appears on the page as a button and communicates with JavaScript to notify the browser of upload status and other events.

### The Event Handlers

Developers must create a set of JavaScript functions that handle SWFUpload events. These functions are called as different important events occur.

By handling the SWFUpload events developers can provide feedback regarding the upload progress, error messages, and upload completion. Developers should not overwrite functions stored in SWFUpload.prototype. Instead create your own set of functions and pass references to them in the settings object.

Example: SWFUpload event handlers and initialization.

`// The uploadStart event handler. This function variable is assigned to upload_start_handler in the settings object var myCustomUploadStartEventHandler = function (file) { var continue_with_upload; if (file.name === "the sky is blue") { continue_with_upload = true; } else { continue_with_upload = false; } return continue_with_upload; }; // The uploadSuccess event handler. This function variable is assigned to upload_success_handler in the settings object var myCustomUploadSuccessEventHandler = function (file, server_data, receivedResponse) { alert("The file " + file.name + " has been delivered to the server. The server responded with " + server_data); }; // Create the SWFUpload Object var swfu = new SWFUpload({ upload_url : "http://www.swfupload.org/upload.php", flash_url : "http://www.swfupload.org/swfupload.swf", file_size_limit : "200 MB", upload_start_handler : myCustomUploadStartEventHandler, upload_success_handler : myCustomUploadSuccessEventHandler });`

## SWFUpload JavaScript Object

### Constructor

SWFUpload(settings object)

Returns: A SWFUpload instance

`var swfupload_instance = new SWFUpload(settings_object);`

### Globals and Constants

Several globals and constants are associated with the SWFUpload object. These are useful for advanced SWFUpload applications and for handling errors. These are considered read-only.

#### SWFUpload.instances

SWFUpload.instances is an object containing a reference to each SWFUpload instance loaded on a page. The Flash Player relies on this object in order to call the correct event handlers. SWFUpload.instances indexes by the movieName property.

#### SWFUpload.movieCount

SWFUpload.movieCount is a global that tracks the number on SWFUpload instances that have been created and helps to ensure each movie is given a unique movieName.

#### SWFUpload.QUEUE_ERROR

SWFUpload.QUEUE_ERROR is a simple object that contains Queue Error code constants. It is generally used to determine which error code was sent in the fileQueueError event.

- QUEUE_LIMIT_EXCEEDED - indicates that the user has attempted to queue more files that is allowed by the settings. Once files have been updated and removed from the queue the user is again allowed to queue additional files.
- FILE_EXCEEDS_SIZE_LIMIT - indicates the selected file is larger than is allwed by the file_size_limit.
- ZERO_BYTE_FILE - indicates that the selected file is empty. The Flash Player cannot handle empty files and the file is rejected. Windows Shortcut files may also trigger this error.
- INVALID_FILETYPE - The selected file's extension does not match a valid extension from the file_types setting. User's can circumvent the file_types restriction by manually entering a file name.

#### SWFUpload.UPLOAD_ERROR

SWFUpload.UPLOAD_ERROR is a simple object that contains Upload Error code constants. It is generally used to determine which error code was sent in the uploadError event.

- HTTP_ERROR - The file upload was attempted but the server did not return a 200 status code.
- MISSING_UPLOAD_URL - The upload_url setting was not set.
- IO_ERROR - Some kind of error occurred while reading or transmitting the file. This most commonly occurs when the server unexpectedly terminates the connection.
- SECURITY_ERROR - The upload violates a security restriction. This error is rare.
- UPLOAD_LIMIT_EXCEEDED - The user has attempted to upload more files than is allowed by the file_upload_limit setting.
- UPLOAD_FAILED - The attempt to initiate the upload caused an error. This error is rare.
- SPECIFIED_FILE_ID_NOT_FOUND - A file ID was passed to startUpload but that file ID could not be found.
- FILE_VALIDATION_FAILED - False was returned from the uploadStart event
- FILE_CANCELLED - cancelUpload was called
- UPLOAD_STOPPED - stopUpload was called.

#### SWFUpload.FILE_STATUS

SWFUpload.FILE_STATUS is a simple object that contains File Status code constants. It can be used to check the file status property on the File object.

- QUEUED - indicates the this file is waiting in the queue.
- IN_PROGRESS - indicates that this file is currently being uploaded.
- ERROR - indicates that this file caused a queue or upload error.
- COMPLETE - indicates that this file was successfully transmitted to the server.
- CANCELLED - indicates that this file was cancelled by a call to cancelUpload.

#### SWFUpload.BUTTON_ACTION

SWFUpload.BUTTON_ACTION is a simple object that contains button action code constants. It is used with the button_action setting to set the behavior of the Flash based file dialog button.

- SELECT_FILE - when the Flash button is clicked the file dialog will only allow a single file to be selected.
- SELECT_FILES - when the Flash button is clicked the file dialog allows multiple files to be selected.
- START_UPLOAD - while the Flash button is clicked the first queued file will be uploaded.

#### SWFUpload.CURSOR

SWFUpload.CURSOR is a simple object that contains button cursor code constants. It is used with the button_cursor setting to set the mouse cursor when hovering over the Flash button.

- ARROW - the cursor will display as an arrow pointer
- HAND - the cursor will display has a finger/hand pointer

#### SWFUpload.WINDOW_MODE

SWFUpload.WINDOW_MODE is a simple object that contains button movie wmode parameter constants. It is used to tell the browser how to display the Flash Movie.

The some WINDOW_MODE/WMODE settings can cause problems in some browsers. See the Known Issues.

- WINDOW is the default mode. The movie is drawn over the top of all other elements on the page.
- OPAQUE causes the movie to be rendered in the page allowing other elements to cover up the button.
- TRANSPARENT the button background is rendered transparent allowing html elements under the button to show through.

### Properties

The following list of properties is intended for your use as described below. Using other properties or writing to read-only properties can cause SWFUpload to malfunction.

#### customSettings (read/write)

The customSettings property is an empty JavaScript object that can be used to store data associated with an instance of SWFUpload. The customSettings object contents can be initialized using the custom_settings setting.

Example:

`// Initialize SWFUpload with some custom settings var swfu = new SWFUpload({ custom_settings : { custom_setting_1 : "custom_setting_value_1", custom_setting_2 : "custom_setting_value_2", custom_setting_n : "custom_setting_value_n", } }); swfu.customSettings.custom_setting_1 = "custom_setting_value_1"; // Change an existing custom setting swfu.customSettings.myNewCustomSetting = "new custom setting value"; // Add a new custom setting // Overwrite the customSettings with a completely new object swfu.customSettings = { custom_setting_A : "custom_setting_value_A", custom_setting_B : "custom_setting_value_B" };`

The values stored in the customSettings object can then be easily accessed in the event handlers:

`function uploadSuccess(file, serverData, receivedResponse) { if (this.customSettings.custom_setting_A === true) { alert("Checked the custom setting!"); } }`

#### movieName

Contains the unique movie name of the SWFUpload instance. This value is passed to Flash and is used to facilitate Flash-to-JavaScript communication. This value is used to index the instance in the SWFUpload.instances array. You should not change the movieName value.

### Methods

The following methods are used to operate SWFUpload. Some are bound to DOM element click event handlers and others are used inside SWFUpload event handlers.

#### object addSetting(setting_name, value, default_value)

Deprecated The addSetting function sets a setting value. If the value is undefined then the default_value is used. The function is used by SWFUpload during initialization. Using addSetting to update a setting will not change the setting in the Flash Control.

addSetting returns the value that was stored in the setting.

#### object getSetting(setting_name)

Deprecated The getSetting function retrieves the value of a setting. If the setting is not found an empty string is returned.

#### object retrieveSetting(setting_value, default_value)

Removed in v2.1.0 The retrieveSetting function is similar to the addSetting function except it does not modify the internal settings object. retrieveSetting returns the setting_value unless it is undefined in which case it returns the default_value

This is a utility function.

#### bool destroy()

Added in v2.1.0

Removes the SWF DOM elements and then destroys SWFUpload internal references. Used for removing a SWFUpload instance from a page. It also attempts to prevent memory leaks in IE.

Returns true if the elements were removed successfully. Returns false if any error occurs leaving the SWFUpload instance in an inconsistent state.

#### void displayDebugInfo()

The displayDebugInfo outputs SWFUpload settings using the debug event. This function is automatically called after initialization if the debug setting is 'true'

#### void selectFile()

Deprecated. Not compatible with Flash Player 10.

selectFile causes the Flash Control to display a File Selection Dialog window. A single file may be selected from the Dialog window.

Calling selectFile begins the File Event Chain.

#### void selectFiles()

Deprecated. Not compatible with Flash Player 10.

selectFiles causes the Flash Control to display a File Selection Dialog window. A multiple files may be selected from the Dialog window.

Calling selectFiles begins the File Event Chain.

#### void startUpload(file_id)

startUpload causes the file specified by the file_id parameter to start the upload process. If the file_id parameter is omitted or undefined then the first file in the queue is uploaded.

Calling startUpload begins the Upload Event Chain.

#### void cancelUpload(file_id, trigger_error_event)

cancelUpload cancels the file specified by the file_id parameter. The file is then removed from the queue.

If the file_id parameter is omitted or undefined then the first file in the queue is cancelled.

The trigger_error_event is optional. If set to false the uploadError event is suppressed.

#### void stopUpload()

stopUpload stops and re-queues the file currently being uploaded.

After the uploading file is stopped the uploadError event is fired. If no file is being uploaded then nothing happens and no event is fired.

#### object getStats()

Retrieves the stats object.

#### void setStats(stats_object)

The Stats Object may be modified. This is useful if you wish to change the number of successful uploads or upload errors after an upload has completed.

#### object getFile(file_id|index)

getFile is used to retrieve a File Object from the queue. The file retrieved by passing in a file id (the id property from a file object) or a file index (the index property from a file object).

When getting a file by file_id only files in the queue are available. If the file is not found null is returned.

When getting a file by index all queued (or files that generated a queue error) are available. If the index is out of range then null is returned

#### void addPostParam(name, value)

The addPostParam function adds a name/value pair that will be sent in the POST for all files uploaded.

The name/value pair will also appear in the post_params setting.

#### void removePostParam(name)

The removePostParam function removes a name/value pair from the values sent with the POST for file uploads.

The name/value pair is also be removed from the post_params setting.

#### bool addFileParam(file_id, name, value)

The addFileParam function adds a name/value pair that will be sent in the POST with the file specified by the file_id parameter.

The name/value pair will only be sent with the file it is added to. To send name/value pairs with all uploads use the post_param setting.

#### bool removeFileParam(file_id, name)

The removeFileParam function removes a name/value pair from a file upload that was added using addFileParam.

If the name/value pair was not found then 'false' is returned.

#### void setUploadURL(url)

Dynamically modifies the upload_url setting.

#### void setPostParams(param_object)

Dynamically modifies the post_params setting. Any previous values are over-written. The param_object should be a simple JavaScript object. All names and values must be strings.

#### void setFileTypes(types, description)

Dynamically updates the file_types and file_types_description settings. Both parameters are required.

#### void setFileSizeLimit(file_size_limit)

Dynamically modifies the file_size_limit setting. This applies to all future files that are queued. The file_size_limit parameter will accept a unit. Valid units are B, KB, MB, and GB. The default unit is KB.

Examples: 2147483648 B, 2097152, 2097152KB, 2048 MB, 2 GB

#### void setFileUploadLimit(file_upload_limit)

Dynamically modifies the file_upload_limit setting. The special value zero (0) indicates "no limit".

#### void setFileQueueLimit(file_queue_limit)

Dynamically modifies the file_queue_limit setting. The special value zero (0) indicates "no limit".

#### void setFilePostName(file_post_name)

Dynamically modifies the file_post_name setting. The Linux Flash Player ignores this setting.

#### void setUseQueryString(use_query_string)

Dynamically modifies the use_query_string setting. When true this forces SWFUpload to send post parameters on the query string rather than in the post. The use_query_string parameter should be a boolean true or false.

#### void setDebugEnabled(debug_enabled)

Dynamically enables or disables debug output. The debug_enabled parameter should be a boolean true or false.

#### void setButtonImageURL(url)

Dynamically change the image used in the Flash Button. The image url must be relative to the swfupload.swf file, an absolute path (e.g., starting with a /), or a fully qualified url (e.g., http://www.swfupload.org/buttonImage.png). Any image format supported by Flash can be loaded. The most notable formats are jpg, gif, and png.

The button image is expected to be a button sprite (or a single image file with several images stacked together). The image will be used to represent all the button states by moving the image up or down to only display the needed portion. These states include: normal, hover, click, disabled. See the sample button images.

#### void setButtonDimensions(width, height)

Dynamically change the Flash button's width and height. The values should be numeric and should not include any units. The height value should be 1/4th of the total button image height so the button state sprite images can be displayed correctly

#### void setButtonText(text)

Sets the text that should be displayed over the Flash button. Text that is too large and overflows the button size will be clipped.

The text can be styled using HTML supported by the Flash Player (Adobe Documentation)

#### void setButtonTextStyle(css_style_text)

Sets the CSS styles used to style the Flash Button Text. CSS should be formatted according to the Flash Player documentation (Adobe Documentation)

Style classes defined here can then be referenced by HTML in the button_text setting.

#### void setButtonTextPadding(left, top)

Sets the top and left padding of the Flash button text. The values may be negative.

#### void setButtonDisabled(isDisabled)

When 'true' changes the Flash Button state to disabled and ignores any clicks.

#### void setButtonAction(buttonAction)

Sets the action taken when the Flash button is clicked. Valid action values are taken from the BUTTON_ACTION constants.

#### void setButtonCursor(buttonCursor)

Sets the mouse cursor shown when hovering over the Flash button. Valid cursor values are taken from the CURSOR constants.

### Events

SWFUpload fires various events during its operation. These events can be handled by the developer to update the UI, change behavior, or report errors.

All SWFUpload events are called in the context of a SWFUpload instance. This means that the 'this' object refers to the SWFUpload instance that fired the event.

SWFUpload events should be set only by assigning the event handler function in the settings object during object initialization. You should not override the internal functions belonging to the SWFUpload.prototype object.

During a file upload events are usually called in this order (the Upload Event Chain):

- uploadStart
- uploadProgress (called over and over again as the file uploads)
- uploadError (called if some kind of error occurs, the file is canceled or stopped)
- uploadSuccess (the upload finished successfully, data returned from the server is available)
- uploadComplete (the upload is complete and SWFUpload is ready to start the next file)

#### flashReady()

flashReady is an internal event that should not be overwritten. It is called by the Flash Control to notify SWFUpload that the Flash movie has loaded and is ready to accept commands.

#### swfUploadLoaded()

The swfUploadLoaded event is fired by flashReady. It is settable. swfUploadLoaded is called to let you know that it is safe to call SWFUpload methods.

#### fileDialogStart()

fileDialogStart is fired after selectFile for selectFiles is called. This event is fired immediately before the File Selection Dialog window is displayed. However, the event may not execute until after the Dialog window is closed.

#### fileQueued(file object)

The fileQueued event is fired for each file that is queued after the File Selection Dialog window is closed.

#### fileQueueError(file object, error code, message)

The fileQueueError event is fired for each file that was not queued after the File Selection Dialog window is closed. 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.

The reason for the queue error is specified by the error code parameter. The error code corresponds to a SWFUpload.QUEUE_ERROR constant.

#### fileDialogComplete(number of files selected, number of files queued, total number of files in the queued)

The fileDialogComplete event fires after the File Selection Dialog window has been closed and all the selected files have been processed. The 'number of files queued' argument indicates the number of files that were queued from the dialog selection (as opposed to the number of files in the queue).

If you want file uploading to begin automatically this is a good place to call 'this.startUpload()'

#### uploadStart(file object)

uploadStart is called immediately before the file is uploaded. This event provides an opportunity to perform any last minute validation, add post params or do any other work before the file is uploaded.

The upload can be cancelled by returning 'false' from uploadStart. If you return 'true' or do not return any value then the upload proceeds. Returning 'false' will cause an uploadError event to fired.

#### uploadProgress(file object, bytes complete, total bytes)

The uploadProgress event is fired periodically by the Flash Control. This event is useful for providing UI updates on the page.

Note: The Linux Flash Player fires a single uploadProgress event after the entire file has been uploaded. This is a bug in the Linux Flash Player that we cannot work around.

#### uploadError(file object, error code, message)

The uploadError event is fired any time an upload is interrupted or does not complete successfully. The error code parameter indicates the type of error that occurred. The error code parameter specifies a constant in SWFUpload.UPLOAD_ERROR.

Stopping, Cancelling or returning 'false' from uploadStart will cause uploadError to fire. Upload error will not fire for files that are cancelled but still waiting in the queue.

#### uploadSuccess(file object, server data, received response)

uploadSuccess is fired when the entire upload has been transmitted and the server returns a HTTP 200 status code. Any data outputted by the server is available in the server data parameter.

Due to some bugs in the Flash Player the server response may not be acknowledged and no uploadSuccess event is fired by Flash. In this case the assume_success_timeout setting is checked to see if enough time has passed to fire uploadSuccess anyway. In this case the received response parameter will be false.

The http_success setting allows uploadSuccess to be fired for HTTP status codes other than 200. In this case no server data is available from the Flash Player.

At this point the upload is not yet complete. Another upload cannot be started from uploadSuccess.

#### uploadComplete(file object)

uploadComplete is always fired at the end of an upload cycle (after uploadError or uploadSuccess). At this point the upload is complete and another upload can be started.

If you want the next upload to start automatically this is a good place to call this.uploadStart(). Use caution when calling uploadStart inside the uploadComplete event if you also have code that cancels all the uploads in a queue.

#### debug(message)

The debug event is called by the SWFUpload library and the Flash Control when the debug setting is set to 'true'. If the debug event is not overridden then SWFUpload writes debug messages to the SWFUpload console (a text box dynamically added to the end of the page body).

### SWFUpload Utility Objects

#### Settings object

The settings object is a JavaScript object that provides the settings for the SWFUpload instance. Each setting should only appear once. Many settings are optional and provide suitable default values if omitted. See the setting details for required and optional settings.

Example:

`{ upload_url : "http://www.swfupload.org/upload.php", flash_url : "http://www.swfupload.org/swfupload.swf", file_post_name : "Filedata", post_params : { "post_param_name_1" : "post_param_value_1", "post_param_name_2" : "post_param_value_2", "post_param_name_n" : "post_param_value_n" }, use_query_string : false, requeue_on_error : false, http_success : [201, 202], assume_success_timeout : 0, file_types : "*.jpg;*.gif", file_types_description: "Web Image Files", file_size_limit : "1024", file_upload_limit : 10, file_queue_limit : 2, debug : false, prevent_swf_caching : false, preserve_relative_urls : false, button_placeholder_id : "element_id", button_image_url : "http://www.swfupload.org/button_sprite.png", button_width : 61, button_height : 22, button_text : "<b>Click</b> <span class="redText">here</span>", button_text_style : ".redText { color: #FF0000; }", button_text_left_padding : 3, button_text_top_padding : 2, button_action : SWFUpload.BUTTON_ACTION.SELECT_FILES, button_disabled : false, button_cursor : SWFUpload.CURSOR.HAND, button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT, swfupload_loaded_handler : swfupload_loaded_function, file_dialog_start_handler : file_dialog_start_function, file_queued_handler : file_queued_function, file_queue_error_handler : file_queue_error_function, file_dialog_complete_handler : file_dialog_complete_function, upload_start_handler : upload_start_function, upload_progress_handler : upload_progress_function, upload_error_handler : upload_error_function, upload_success_handler : upload_success_function, upload_complete_handler : upload_complete_function, debug_handler : debug_function, custom_settings : { custom_setting_1 : "custom_setting_value_1", custom_setting_2 : "custom_setting_value_2", custom_setting_n : "custom_setting_value_n", } }`

#### Settings Description

##### upload_url

The upload_url setting accepts a full, absolute, or relative target URL for the uploaded file. Relative URLs should be relative to document. The upload_url should be in the same domain as the Flash Control for best compatibility.

If the preserve_relative_urls setting is false SWFUpload will convert the relative URL to an absolute URL to avoid the URL being interpreted differently by the Flash Player on different platforms. If you disable SWFUploads conversion of the URL relative URLs should be relative to the swfupload.swf file.

##### file_post_name

The file_post_name allows you to set the value name used to post the file. This is not related to the file name. The default value is 'Filedata'. For maximum compatibility it is recommended that the default value is used.

##### post_params

The post_params setting defines the name/value pairs that will be posted with each uploaded file. This setting accepts a simple JavaScript object. Multiple post name/value pairs should be defined as demonstrated in the sample settings object. Values must be either strings or numbers (as interpreted by the JavaScript typeof function).

Note: Flash Player 8 does not support sending additional post parameters. SWFUpload will automatically send the post_params as part of the query string.

##### use_query_string

The use_query_string setting may be true or false. This value indicates whether SWFUpload should send the post_params and file params on the query string or the post. This setting was introduced in SWFUpload v2.1.0.

##### preserve_relative_urls

A boolean value that indicates whether SWFUpload should attempt to convert relative URLs used by the Flash Player to absolute URLs. If set to true SWFUpload will not modify any URLs. The default value is false.

##### requeue_on_error

The requeue_on_error setting may be true or false. When this setting is true any files that has an uploadError (excluding fileQueue errors and the FILE_CANCELLED uploadError) is returned to the front of the queue rather than being discarded. The file can be uploaded again if needed. To remove the file from the queue the cancelUpload method must be called.

All the events associated with a failed upload are still called and so the requeuing the failed upload can conflict with the Queue Plugin (or custom code that uploads the entire queue). Code that automatically uploads the next file in the queue will upload the failed file over and over again if care is not taken to allow the failing upload to be cancelled.

This setting was introduced in SWFUpload v2.1.0.

##### http_success

An array that defines the HTTP Status Codes that will trigger success. 200 is always a success. Also, only the 200 status code provides the serverData.

When returning and accepting an HTTP Status Code other than 200 it is not necessary for the server to return content.

##### assume_success_timeout

The number of seconds SWFUpload should wait for Flash to detect the server's response after the file has finished uploading. This setting allows you to work around the Flash Player bugs where long running server side scripts causes Flash to ignore the server response or the Mac Flash Player bug that ignores server responses with no content.

Testing has shown that Flash will ignore server responses that take longer than 30 seconds after the last uploadProgress event.

A timeout of zero (0) seconds disables this feature and is the default value. SWFUpload will wait indefinitely for the Flash Player to trigger the uploadSuccess event.

##### file_types

The file_types setting accepts a semi-colon separated list of file extensions that are allowed to be selected by the user. Use '*.*' to allow all file types.

##### file_types_description

A text description that is displayed to the user in the File Browser dialog.

##### file_size_limit

The file_size_limit setting defines the maximum allowed size of a file to be uploaded. This setting accepts a value and unit. Valid units are B, KB, MB and GB. If the unit is omitted default is KB. A value of 0 (zero) is interpreted as unlimited.

Note: This setting only applies to the user's browser. It does not affect any settings or limits on the web server.

##### file_upload_limit

Defines the number of files allowed to be uploaded by SWFUpload. This setting also sets the upper bound of the file_queue_limit setting. Once the user has uploaded or queued the maximum number of files she will no longer be able to queue additional files. The value of 0 (zero) is interpreted as unlimited. Only successful uploads (uploads the trigger the uploadSuccess event) are counted toward the upload limit. The setStats function can be used to modify the number of successful uploads.

Note: This value is not tracked across pages and is reset when a page is refreshed. File quotas should be managed by the web server.

##### file_queue_limit

Defines the number of unprocessed files allowed to be simultaneously queued. Once a file is uploaded, errored, or cancelled a new files can be queued in its place until the queue limit has been reached. If the upload limit (or remaining uploads allowed) is less than the queue limit then the lower number is used.

##### flash_url

The full, absolute, or relative URL to the Flash Control swf file. This setting cannot be changed once the SWFUpload has been instantiated. Relative URLs are relative to the page URL.

##### flash_width

(Removed in v2.1.0) Defines the width of the HTML element that contains the flash. Some browsers do not function correctly if this setting is less than 1 px. This setting is optional and has a default value of 1px.

##### flash_height

(Removed in v2.1.0) Defines the height of the HTML element that contains the flash. Some browsers do not function correctly if this setting is less than 1 px. This setting is optional and has a default value of 1px.

##### flash_color

Removed in v2.2.0 This setting sets the background color of the HTML element that contains the flash. The default value is '#FFFFFF'.

Note: This setting may not be effective in "skinning" 1px flash element in all browsers.

##### prevent_swf_caching

Added in v2.2.0 This boolean setting indicates whether a random value should be added to the Flash URL in an attempt to prevent the browser from caching the SWF movie. This works around a bug in some IE-engine based browsers.

Note: The algorithm for adding the random number to the URL is dumb and cannot handle URLs that already have some parameters.

##### debug

A boolean value that defines whether the debug event handler should be fired.

##### button_placeholder_id

(Added in v2.2.0) This required setting sets the ID of DOM element that will be replaced by the Flash Button. This setting overrides the button_placeholder setting. The Flash button can be styled using the CSS class 'swfupload'.

##### button_placeholder

(Added in v2.2.0) This required setting sets the DOM element that will be replaced by the Flash Button. This setting is only applied if the button_placeholder_id is not set. The Flash button can be styled using the CSS class 'swfupload'.

##### button_image_url

(Added in v2.2.0) Fully qualified, absolute or relative URL to the image file to be used as the Flash button. Any Flash supported image file format can be used (another SWF file or gif, jpg, or png).

This URL is affected by the preserve_relative_urls setting and should follow the same rules as the upload_url setting.

The button image is treated as a sprite. There are 4 button states that must be represented by the button image. Each button state image should be stacked above the other in this order: normal, hover, down/click, disabled.

##### button_width

(Added in v2.2.0) A number defining the width of the Flash button.

##### button_height

(Added in v2.2.0) A number defining the height of the Flash button. This value should be 1/4th of the height or the button image.

##### button_text

(Added in v2.2.0) Plain or HTML text that is displayed over the Flash button. HTML text can be further styled using CSS classes and the button_text_style setting. See Adobe's Flash documentation for details.

##### button_text_style

(Added in v2.2.0) CSS style string that defines how the button_text is displayed. See Adobe's Flash documentation for details.

##### button_text_top_padding

(Added in v2.2.0) Used to vertically position the Flash button text. Negative values may be used.

##### button_text_left_padding

(Added in v2.2.0) Used to horizontally position the Flash button text. Negative values may be used.

##### button_action

(Added in v2.2.0) Defines the action taken when the Flash button is clicked. Valid action values can be found in the swfupload.js file under the BUTTON_ACTION object.

##### button_disabled

(Added in v2.2.0) A boolean value that sets whether the Flash button is in the disabled state. When in the disabled state the button will not execute any actions.

##### button_cursor

(Added in v2.2.0) Used to define what type of mouse cursor is displayed when hovering over the Flash button.

##### button_window_mode

(Added in v2.2.0) Sets the WMODE property of the Flash Movie. Valid values are available in the SWFUpload.WINDOW_MODE constants.

##### custom_settings

The custom_settings setting allows developers to safely attach additional information to a SWFUpload instance without worrying about affecting internal SWFUpload values or changes in new SWFUpload versions. This setting accepts a JavaScript object.

Once instantiated the custom settings are accessed in the 'customSettings' property of the SWFUpload instance.

`var swfu = new SWFUpload({ custom_settings : { "My Setting" : "This is my setting", myothersetting : "This is my other setting", integer_setting : 100, a_dom_setting : document.getElementById("some_element_id") } }); var my_setting = swfu.customSettings["My Setting"]); swfu.customSettings["My Setting"] = "This is my new setting"; swfu.customSetting.myothersetting = "another new value"; swfu.customSetting.integer_setting += 25; swfu.customSetting["a_dom_setting"].style.visibility = "hidden";`

##### Event Handlers

The remaining settings define the event handlers called by SWFUpload during its operation. JavaScript functions should be defined to handle these events as needed.

#### File Object

The file object is passed to several event handlers. It contains information about the file. Some operating systems do not fill in all the values (this is especially true for the createdate and modificationdate values).

`{ id : string, // SWFUpload file id, used for starting or cancelling and upload index : number, // The index of this file for use in getFile(i) name : string, // The file name. The path is not included. 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 SWFUpload.FILE_STATUS to interpret the value. }`

#### Stats Object

The Stats object provides information about the upload queue.

That stats object contains the following properties:

`{ in_progress : number // 1 or 0 indicating if a file upload is currently in progress files_queued : number // The number of files currently in the queue successful_uploads : number // The number of files that have uploaded successfully (caused uploadSuccess to be fired) upload_errors : number // The number of files that have had errors (excluding cancelled files) upload_cancelled : number // The number of files that have been cancelled queue_errors : number // The number of files that caused fileQueueError to be fired }`

All these values can be updated using setStats() except the in_progress and files_queued values.

## SWFUpload Plug-ins

With SWFUpload v2.0 several plugins have been introduced. They are provided to help with common tasks associated with implementing SWFUpload.

Currently most of the documentation for using the plugins is contained in the plugin JavaScript file.

### SWFObject

The SWFObject plugin uses the SWFObject library to handle the embedding of the SWFUpload Flash Component into the page.

This plugin also provides support for Document Ready loading and Flash Version Detection. Usage details are documented in the plugin file itself. You should not use the SWFObject's Document Ready loading mixed with another libraries DOMReady. Use one or the other but not both.

Flash Player 10: Because Flash Player 10 requires the SWFUpload swf to act is a button the movie must be visible in order for it to load. If the button_placeholder_id is set to an element that is hidden (visibility set to hidden or display set to none) SWFUpload will fail to load.

### Cookies

In response to the Flash Cookie Bug the Cookies Plugin automatically retrieves your browser's cookies and sends them with the upload. The are sent as POST or GET variables to the upload url.

Note that this plugin sends the cookies name/values in the POST or GET. On the server side they will not be accessible as cookies. Some frameworks that automatically check cookies for session or authentication values still will not be able to find the values.

### Queue Handling

This plugin provides Queue Handling features such as entire queue uploading, entire queue cancelling and automatic starting of uploads after being queued.

### Speed

This Plugin extends the 'file' object with several properties that describe the current upload. This includes current speed, average speed, elapsed time, remaining time and more.

## Known Issues

The Flash Player and many Browsers have bugs that have a direct impact on the performance of SWFUpload. While we have worked hard to get around many issues but there are some things that we cannot fix.

### Cancelling in Linux

Older Flash 9 Players for Linux cause the browser to crash if an upload is cancelled. Newer Flash 9 Players behave better.

### Upload Progress in Linux

The Flash Player in Linux sends a single uploadProgress event after the file has finished uploading.

In some distributions the entire browser locks up while the upload is in progress.

### Upload Progress in OS X

There have been some reports that uploadProgress events are not fired in MAC Flash Players. The specifics haven't been pinned down but be aware of the possible issue.

### MIME Type

The Flash Player uploads all files with a mime type of application/octet-stream regardless of the file's actual mime type.

### Maximum number of selected files

The Flash Player does not impose a maximum number of selected files. However, it builds a selected files string which does have a maximum length. The string is built using the file's name and the separator [quote][space][quote]. The total number of files selected is determined by the sum of the lengths of the file names and a prefixed and postfixed [quote] character (2 characters) and the number of files selected minus one times 3 (for the separator string)

This limitation may vary from system to system. If a use selects too many files they will be receive a Flash Player generated warning message and will be left at the File Selection Dialog.

### Proxies

The Flash Player may not properly use proxies. It does not handle authenticating proxies well (if at all) and will some-times crash.

Some anti-virus software uses a proxy to scan uploads and cause SWFUpload to believe the entire file has been uploaded. SWFUpload will fire uploadProgress events very quickly until it reaches 100% and will then seem to pause until the proxy completes uploading the file to the server.

### Apache mod_security

Apache's mod_security validates POST to the server. Flash Player has implemented an edge case (there is argument as to whether it is invalid or note) POST for file uploads and so servers implementing mod_security will reject the upload. mod_security can be disabled using your .htaccess file

### SSL

There have been some reports that the Flash Player cannot upload through SSL. The issue has not been pinned down but uploading over SSL may be unreliable. There especially seems to be an issue with using self-signed certificates.

Also, SSL tickets from untrusted Certificate Authorities (CA) do not work as Flash does not provide a method for accepting the certificate. It has been noted that, like the cookie bug, that Flash Player on Windows obtains its trusted CA list from Internet Explorer regardless of the browser in use.

### Authentication

HTTP Authentication is not well supported by the Flash Player. Later versions of Flash Player behave better. Old version of Flash Player would crash the browser.

### Prematurely terminated connections

Prematurely ending the response (such as a Response.end() in ASP.Net) can sometimes cause successful uploads to be reported as failed.

### Filedata in Linux

Changing the Filedata value (file_post_name setting) is ignored in Linux Flash Players.

### Cookie issue

On Windows the Non-IE Flash Player plugin (FireFox, Opera, Safari, etc) will send the IE cookies regardless of the browser used. This breaks authentication and sessions for many server-side scripting technologies.

Developers should manually pass Session and Authentication cookie information and manually restore Sessions on the Server Side if they wish to use Sessions

The SWFUpload package contains work-around sample code for PHP and ASP.Net

### ExternalInterface bugs

Flash Player does not properly escape data when communication with the browser/JavaScript. SWFUpload goes to great lengths to work-around this issue. If this bug is fixed in future Flash Players/Browsers then SWFUpload will send extra escaped data.

### Server Data length bugs

Very long server data is corrupted on Mac and Linux Flash Players. Server data will be truncated, garbled, and/or repeated. We recommend keeping data returned from the server short and concise.

### Avant Browser

For some users the Avant Browser does not work with SWFUpload after the Flash Control has been cached. This has been reproduced by the SWFUpload developers but the Avant Browser developers did not experience any problems.

When the page is reloaded SWFUpload loads and fires the swfupload_loaded event. However, none of the ExternalInterface callback functions are defined on the movie element. SWFUpload v2.0.2 has added checks which prevent swfupload_loaded from firing if the callback functions are not detected.

SWFUpload v2.2.0 added the prevent_swf_caching setting that attempts to work around this issue.

### File Dialog & Page Changing

Leaving or reloading the current page while the File Browser Dialog window is open will cause the browser to crash (all browsers, all OSs). Most commonly this is caused by failing to cancel a click event on an <a> tag where the onclick event calls the selectFile or selectFiles function.

### Long Running Upload Scripts

After Flash has uploaded the file to the webserver the upload script is executed. This script handles the file whether that means saving it, creating a thumbnail, scanning for viruses, etc. If the upload script does not return any data within 30 seconds Flash will disconnect and return an IO Error. You can prevent this by returning characters or data while the script runs (if possible). For PHP, the script continues to run and complete successfully even though Flash has terminated the connection. Any data returned by the script after Flash has disconnected is lost.

### WMODE / BUTTON_WINDOW_MODE

In some browsers the selected WMODE (which is set using the BUTTON_WINDOW_MODE) can prevent the Flash Control from loading if the control is not on screen. The control will finally load once the user scrolls the page so the control becomes visible.

This behavior can adversely affect the SWFObject plugin. No SWFUpload events will be fire and the button image will not be loaded until the control becomes visible.

### Memory Leaks

Some browsers (especially IE) cannot recover memory used by the Flash Player when JavaScript communication via the ExternalInterface classes is used (like in SWFUpload). Creating many SWFUpload instances and/or reloading the page several times will cause the browser to consume more and more memory until it crashes or otherwise harms the system.

In v2.2.0 SWFUpload we gave implemented some preventative measures. Some of these measures are pro-active but it is still recommended that you call the destroy method when the page unloads. If you are using hundreds of SWFUpload instances on a page you should use caution and test carefully for memory leaks.

### Other Mac Issues

- Users have reported that uploading to subdomains does not work with the Mac Flash Player.
- Users have reported that pages that redirect (HTTP Status 302) are not handled by the Mac Flash Player. Windows clients seem to handle this issue.
- The flash documentation states that on OS early than OS X 10.3 the bytes loaded is always reported as -1. SWFUpload converts this to 0 but the total bytes will never be sent and 100% won't be reached. The UI should be updated to display 100% complete in uploadSuccess or uploadComplete events to maintain a consistent UI.
- Some users have reported issues if there is a space character in the upload_url for the Mac Flash Player. Avoid spaces or try replacing them with + or %20.
- Users have reported that the Flash Player for Mac adds the PORT to the HTTP_HOST server variable (e.g., http://www.example.com:80). If you are checking this variable in your server-side script be aware of the possible issue.