SWFUploadIt is a file upload plugin that combines Flash and JavaScript, and its functionality is very powerful. I have used it several times in projects before, but its configuration parameters are too many. After using it once, I forget how to use it, and when I need it later, I have to go to the official website to read its documentation, which is really annoying. So I simply recorded its usage, which is also convenient for students with poor English to refer to. It benefits both myself and others, and solves the problem once and for all.
Features of SWFUpload
- 1. Upload with Flash, no page refresh, and the Flash button style can be customized;
- 2. Files to be uploaded can be restricted on the browser side;
- 3. Allows uploading multiple files at once, but there is an upload queue. Files in the queue are uploaded one by one. The server-side receives files in the same way as a normal form file upload;
- 4. Provides rich event interfaces for developers to use;
SWFUpload file upload process:
- 1. Include the corresponding JS file
- 2. Instantiate the SWFUpload object, passing a configuration parameter object for various configurations.
- 3. Click the Flash button provided by SWFUpload, and a file selection window pops up to select files to upload;
- 4. After file selection, the files that meet the requirements will be added to the upload queue;
- 5. Call the startUpload method to start uploading files in the queue;
- 6. During the file upload process, corresponding events are triggered. Developers use these events to update the UI, handle errors, show prompts, and so on;
SWFUpload consists of three parts: SWFUpload.js, swfupload.swf, initialization configuration parameters, and various event handling functions. So first include SWFUpload.js in the page
<script src='SWFUpload.js.html'></script>
Then instantiate a SWFUpload object:
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_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,
};
swfu = new SWFUpload(settings_object);//实例化一个SWFUpload,传入参数配置对象
};
/*定义各种事件监听函数*/
function swfupload_loaded_function(){}
function file_dialog_start_function(){}
...等等
We can see that implementing a SWFUpload upload function is very simple: just instantiate a SWFUpload object. But the troublesome part is the configuration parameter object used during instantiation, as well as the timing of various events and the parameters they provide. So here comes the key point. The following tables list the things commonly used in development. Although there are already quite a lot, they are not everything in SWFUpload. I have only listed the commonly used ones. For the complete documentation, please refer to the official SWFUpload website.
Common attributes and descriptions in the SWFUpload configuration parameter object
| Property | Type | Default value | Description |
| upload_url | String | The URL address of the server-side page that processes the uploaded file. It can be an absolute address or a relative address. When it is a relative address, it is relative to the document address where the current code resides. | |
| preserve_relative_urls | Boolean | false | If false, SWFUpload will convert relative addresses used by swfupload.swf into absolute addresses for better compatibility. |
| file_post_name | String | Filedata | Equivalent to the name attribute when uploading files with a normal file field. The server-side receiving page uses this name to obtain the uploaded file. |
| post_params | Object (literal) | An object literal. Its key/value pairs will be uploaded along with each file. It is very useful when you need to attach additional information to file uploads. | |
| use_query_string | Boolean | false | When false, the parameters defined by the post_params attribute will be uploaded via POST; when true, they will be uploaded via GET (that is, the parameters will be appended to the URL as a query string). |
| file_types | String | This attribute specifies the allowed file types for upload. When there are multiple types, separate them with semicolons, for example: *.jpg;*.png. Use *.* to allow all types. | |
| file_types_description | String | Specifies the file type description displayed in the file selection window, which serves as a hint and explanation. | |
| file_size_limit | String | Specifies the maximum size of the file to upload. Units are allowed. Valid units are: B, KB, MB, GB. If the unit is omitted, the default is KB. When this attribute is 0, it means there is no limit on file size. | |
| file_upload_limit | Number | Specifies the maximum number of files that can be uploaded. When the number of successfully uploaded files reaches this maximum, no more files can be uploaded, nor can files be added to the upload queue. Setting this attribute to 0 means there is no limit on the number of uploaded files. | |
| file_queue_limit | Number | Specifies the maximum number of files that can be stored in the file upload queue at the same time. When this number is exceeded, only after an equal number of files in the queue have been uploaded successfully, have encountered an error, or have been canceled can other files be added. When the value of file_upload_limit or the remaining number of files that can be uploaded is less than file_queue_limit, the smaller value is taken. | |
| flash_url | String | The absolute or relative address of the swfupload.swf file. A relative address is relative to the current page address. After instantiating SWFUpload, the value of this attribute can no longer be changed. | |
| prevent_swf_caching | Boolean | When true, a random number will be added after the swfupload.swf address to prevent the Flash movie from being cached. This is to prevent bugs that occur in some versions of IE browsers when reading cached Flash movies. | |
| button_placeholder_id | String | Specifies the id of a DOM element. This DOM element will be replaced by the Flash button after SWFUpload is instantiated. This DOM element acts as a placeholder. | |
| button_placeholder | DOMElement | Specifies a DOM element. This DOM element will be replaced by the Flash button after SWFUpload is instantiated. This DOM element acts as a placeholder. When both button_placeholder_id and button_placeholder exist, button_placeholder_id takes precedence. | |
| button_image_url | String | Specifies the background image of the Flash button. Either a relative address or an absolute address is acceptable. This address is affected by the preserve_relative_urls attribute and follows the same rules as upload_url. The background image must be a sprite image. From top to bottom, it contains the four states of the Flash button: normal, mouse hover, pressed, and disabled. Therefore, the height of this image should be four times the height of the Flash button. | |
| button_width | Number | Specifies the width of the Flash button. | |
| button_height | Number | Specifies the height of the Flash button. It should be 1/4 of the height of the button background image specified by button_image_url. | |
| button_text | String | Specifies the text on the Flash button. It can also be HTML code. | |
| button_text_style | String | The style of the text on the Flash button. See the example for usage. | |
| button_text_top_padding | Number | Specifies the top padding of the Flash button. Negative values can be used. | |
| button_text_left_padding | Number | Specifies the left padding of the Flash button. Negative values can be used. | |
| button_disabled | Boolean | false | When true, the Flash button will become disabled, and clicking it will not trigger any behavior. |
| button_cursor | Specifies the cursor style when the mouse hovers over the Flash button. The available values are the constants defined in SWFUpload.CURSOR. | ||
| button_window_mode | Specifies the WMODE attribute of the Flash button. The available values are the constants defined in SWFUpload.WINDOW_MODE. | ||
| file_dialog_start_handler | Function | fileDialogStart event listener function | |
| file_queued_handler | Function | fileQueued event listener function | |
| file_queue_error_handler | Function | fileQueueError event listener function | |
| file_dialog_complete_handler | Function | fileDialogComplete event listener function | |
| upload_start_handler | Function | uploadStart event listener function | |
| upload_progress_handler | Function | uploadProgress event listener function | |
| upload_error_handler | Function | uploadError event listener function | |
| upload_success_handler | Function | uploadSuccess event listener function | |
| upload_complete_handler | Function | uploadComplete event listener function |
Explanation of various SWFUpload events
| fileDialogStart ( ) | ||
| Triggered when the file selection window is about to pop up. | ||
| fileQueued ( file object ) | ||
| This event is triggered when a file is added to the upload queue. The only parameter provided is the file object containing the file information. | ||
| fileQueueError ( file object, error code, message ) | ||
| This event is triggered when a file fails to be added to the upload queue. The reasons for failure may be that the file size exceeds the allowed value, the file is empty, or the file queue is already full, etc. This event provides three parameters. The first parameter is the file object that currently has the problem, and the second parameter is the specific error code, which can refer to the constants defined in SWFUpload.QUEUE_ERROR. | ||
| fileDialogComplete ( number of files selected, number of files queued, total number of files in the queued ) | ||
| This event is triggered immediately after file selection is completed and the selected files have been processed (that is, added to the upload queue). You can call the this.startUpload() method in this event to implement automatic file upload. The parameter number of files selected refers to the number of files selected in the file selection dialog this time. The parameter number of files queued refers to the number of files added to the upload queue this time. The parameter total number of files in the queued refers to how many files are currently in the upload queue (including the files added this time). | ||
| uploadStart ( file object ) | ||
| This event is triggered when a file is about to be uploaded. This event gives you the last chance before the file is uploaded to validate file information, add additional information to be uploaded with the file, or do other work. You can cancel the upload of this file by returning false. The parameter file object is the information object of the file currently being uploaded. | ||
| uploadProgress ( file object, bytes complete, total bytes ) | ||
| This event is triggered repeatedly during the file upload process. You can use this event to implement an upload progress bar. The parameter file object is the file information object. The parameter bytes complete is the number of bytes that have been uploaded so far. The parameter total bytes is the total number of bytes in the file. | ||
| uploadError ( file object, error code, message ) | ||
| This event is triggered when the file upload is interrupted or the file fails to upload successfully. Stopping or canceling the file upload, or returning false in the uploadStart event, will trigger this event. However, if a file is canceled but still remains in the queue, this event will not be triggered. The parameter file object is the file information object. The parameter error code is the error code. For details, refer to the constants defined in SWFUpload.UPLOAD_ERROR. | ||
| uploadSuccess ( file object, server data, received response ) | ||
| This event is triggered after a file is uploaded successfully. The parameter file object is the file information object. The parameter server data is the data output by the server. | ||
| uploadComplete( file object ) | ||
| This event is triggered when a file upload process is completed (whether successful or not). This event indicates that the current upload has been completed and the next file in the upload queue can begin uploading. After this event occurs, the upload of the next file in the queue will start. |
Methods of the SWFUpload instance
Most of the methods dynamically change the parameter configuration object.
| destroy ( ) | ||
| When SWFUpload is no longer needed, you can use this method to destroy its instance and DOM elements. | ||
| startUpload( file_id ) | ||
| Start uploading the specified file in the queue. The parameter file_id represents the id of the file to be uploaded. If this parameter is not provided, the first file in the queue will be uploaded. | ||
| cancelUpload ( file_id, trigger_error_event ) | ||
| Cancel the upload of a file. The parameter file_id is the id of the file to be canceled. If this parameter is undefined or not provided, the first file in the queue will be canceled. The parameter trigger_error_event accepts a boolean value. When it is false, canceling a file will not trigger the uploadError event. The default is true. | ||
| stopUpload ( ) | ||
| Terminate the currently uploading file. This will trigger the uploadError event. If no file is currently uploading, this method does nothing. | ||
| getStats ( ) | ||
| Get the stats object of the queue. | ||
| setStats ( stats_object ) | ||
| Modify the stats_object of the queue. Pass the modified stats_object as a parameter. | ||
| getFile ( file_id|index ) | ||
| Get a File Object based on the file id or file index. When using the file id, only files in the queue can be obtained. When using the file index, all files (including those in and out of the queue) can be obtained. | ||
| addPostParam ( name, value) | ||
| Add a key/value pair to the additional information object specified by post_params in the configuration object. | ||
| removePostParam ( name) | ||
| Remove a key/value pair contained in post_params in the configuration object. The parameter name is the key name of the value to be removed. | ||
| addFileParam ( file_id, name, value) | ||
| Add additional information to be uploaded together with a specific file. Note that the additional information will only be uploaded when the specified file is uploaded. The additional information set in post_param in the configuration object will be sent together with any file upload. The parameter file_id is the id of the specified file. The parameters name and value are the name and value of the additional information, respectively. | ||
| removeFileParam ( file_id, name) | ||
| Remove the additional information added via the addFileParam method. I believe I don't need to explain the two parameters further. | ||
| setUploadURL ( url) | ||
| Dynamically set the value of upload_url in the configuration object. | ||
| setPostParams ( param_object) | ||
| Dynamically set the value of the post_params property in the configuration object. The new value will overwrite the old value. The parameter param_object must be an object literal, and its properties and values can only be strings. | ||
| setFileTypes ( types, description) | ||
| Dynamically set the values of the file_types and file_types_description properties in the configuration object. Neither parameter can be omitted. | ||
| setFileSizeLimit ( file_size_limit) | ||
| Dynamically set the value of the file_size_limit property in the configuration object. | ||
| setFileUploadLimit ( file_upload_limit) | ||
| Dynamically set the value of the file_upload_limit property in the configuration object. | ||
| setFileQueueLimit ( file_queue_limit) | ||
| Dynamically set the value of the file_queue_limit property in the configuration object. | ||
| setFilePostName ( file_post_name) | ||
| Dynamically set the value of the file_post_name property in the configuration object. | ||
| setUseQueryString ( use_query_string) | ||
| Dynamically set the value of the use_query_string property in the configuration object. | ||
| setButtonImageURL ( url) | ||
| Dynamically set the value of the button_image_url property in the configuration object. | ||
| setButtonDimensions ( width, height) | ||
| Dynamically set the width and height of the Flash button. The two parameters are the width and height values, of numeric type, and cannot include units. | ||
| setButtonText ( text) | ||
| Dynamically set the value of the button_text property in the configuration object. | ||
| setButtonTextStyle ( css_style_text) | ||
| Dynamically set the value of the button_text_style property in the configuration object. | ||
| setButtonTextPadding ( left, top ) | ||
| Dynamically set the left and top padding of the Flash button. | ||
| setButtonDisabled ( isDisabled ) | ||
| Dynamically enable or disable the Flash button. The parameter is a boolean value. | ||
| setButtonCursor ( buttonCursor ) | ||
| Dynamically set the value of button_cursor in the configuration object. |
File Information Object (File Object)
In event listener functions, the file information object is often used to obtain file information for the next step.
| Property | Type | Description |
| id | String | The file id defined by SWFUpload, used to control file uploads. |
| index | Number | The file index, used in the getFile(i) method. |
| name | String | The original name of the file, not including the path. |
| type | String | File type |
| creationdate | Date | The creation date of the file |
| modificationdate | Date | The last modified date of the file |
| filestatus | Number | The current status of the file. For details, refer to the constants defined in SWFUpload.FILE_STATUS. |
Queue Status Object (Stats Object)
Used to obtain the current status of the queue.
| Property | Type | Description |
| in_progress | Number | The value is 1 or 0, indicating whether a file is currently being uploaded in the queue. |
| files_queued | Number | The number of files currently in the upload queue. |
| successful_uploads | Number | The number of files successfully uploaded (meaning the uploadSuccess event was triggered). |
| upload_errors | Number | The number of files that failed to upload (including canceled uploads). |
| upload_cancelled | Number | The number of files whose uploads were canceled. |
| queue_errors | Number | The number of files that triggered the fileQueueError event. |
Some constants
Some constants defined by SWFupload for easier understanding.
| Constant name | Description |
| SWFUpload.instances | This constant is an object that represents a collection of references to all SWFUpload instances on a page, indexed by the movieName property of the SWFUpload instance. |
| SWFUpload.movieCount | The number of SWFUpload instances present on the page. |
| SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED | The user selected more files than the allowed number. |
| SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT | The file size exceeds the allowed size. |
| SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE | The file is empty. |
| SWFUpload.QUEUE_ERROR.INVALID_FILETYPE | The file type is not allowed. |
| SWFUpload.UPLOAD_ERROR.HTTP_ERROR | The status code returned by the server is not 200. |
| SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL | upload_url is not set. |
| SWFUpload.UPLOAD_ERROR.IO_ERROR | An error occurred while reading or transferring the file. |
| SWFUpload.UPLOAD_ERROR.SECURITY_ERROR | The upload is restricted by security. |
| SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED | The number of uploaded files exceeds the allowed maximum. |
| SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED | An upload error occurred. |
| SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND | The file id passed to the startUpload() method does not exist. |
| SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED | false was returned in the uploadStart() method. |
| SWFUpload.UPLOAD_ERROR.FILE_CANCELLED | The upload was canceled. |
| SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED | The upload was terminated. |
| SWFUpload.FILE_STATUS.QUEUED | The file is waiting in the queue to be uploaded. |
| SWFUpload.FILE_STATUS.IN_PROGRESS | The file is uploading. |
| SWFUpload.FILE_STATUS.ERROR | An error occurred when the file was added to the queue or uploaded. |
| SWFUpload.FILE_STATUS.COMPLETE | The file was uploaded successfully. |
| SWFUpload.FILE_STATUS. | The file upload was canceled. |
| SWFUpload.CURSOR.ARROW | The mouse is displayed as an arrow. |
| SWFUpload.CURSOR.HAND | The mouse is displayed as a hand. |
| SWFUpload.WINDOW_MODE.WINDOW | The Flash button will be displayed above all DOM elements on the page. |
| SWFUpload.WINDOW_MODE.OPAQUE | Allow other DOM elements to cover the Flash button. |
| SWFUpload.WINDOW_MODE.TRANSPARENT | Allow the Flash button to be displayed transparently. |
Download URL
- SWFupload download address:https://code.google.com/p/swfupload/
- SWFUpload official documentation (version 2.5.0):http://www.w3cschool.cc/w3cnote/swfupload-document.html