Create new RichFaces Documentation Jira issue

This will launch the RichFaces Jira page - to complete your feedback please login if needed, and submit the Jira.

JBoss.orgCommunity Documentation

6.11.5.  < rich:fileUpload > available since 3.2.0

Table 6.404. rich : fileUpload attributes

Attribute Name Description
acceptedTypesFiles types allowed to upload
accesskeyHTML: This attribute assigns an access key to an element. An access key is a single character from the document character set. Note: Authors should consider the input method of the expected reader when specifying an accesskey
addButtonClassAssigns one or more space-separated CSS class names to the component 'Add' button
addButtonClassDisabledAssigns one or more space-separated CSS class names to the component 'Add' button disabled
addControlLabelDefines a label for an add button
ajaxSingleBoolean attribute which provides possibility to limit JSF tree processing(decoding, conversion/validation, value applying) to the component which send the request only. Default value is "false"
allowFlashAttribute which allow the component to use the flash module that provides file upload functionality [false, true, auto]. Default value is "false"
altHTML: For a user agents that cannot display images, forms, or applets, this attribute specifies alternate text. The language of the alternate text is specified by the lang attribute
autoclearIf this attribute is "true" files will be immediately removed from list after upload completed. Default value is "false".
binding JSF: The attribute takes a value-binding expression for a component property of a backing bean
cancelEntryControlLabelDefines a label for a cancel control
cleanButtonClassAssigns one or more space-separated CSS class names to the component 'Clean' button
cleanButtonClassDisabledAssigns one or more space-separated CSS class names to the component 'Clean' button disabled
clearAllControlLabelDefines a label for a clearAll button
clearControlLabelDefines a label for a clear control
disabledHTML: Attribute 'disabled' provides a possibility to make the whole component disabled if its value equals to "true". Default value is "false".
doneLabelDefines a label for a done label
fileEntryClassAssigns one or more space-separated CSS class names to the file entries
fileEntryClassDisabledAssigns one or more space-separated CSS class names to the file entries disabled
fileEntryControlClassAssigns one or more space-separated CSS class names to the controls of the file entries
fileEntryControlClassDisabledAssigns one or more space-separated CSS class names to the disabled controls of the file entries
fileUploadListenerMethodExpression representing an action listener method that will be notified after file uploaded.
id JSF: Every component may have a unique id that is automatically created if omitted
immediateA flag indicating that this component value must be converted and validated immediately (that is, during Apply Request Values phase), rather than waiting until a Process Validations phase
immediateUploadIf this attribute is true files will be immediately uploaded after they have been added in list. Default value is "false".
listHeightDefines height of file list. Default value is "210px".
listWidthDefines width of file list. Default value is "400px".
localeUsed for locale definition
maxFilesQuantityDefines max files count allowed for upload (optional). Default value is "1".
noDuplicateDefines if component should allow to add files that were already in list. Default value is "false".
onaddThe client-side script method to be called before a file is added
onblur DHTML: The client-side script method to be called when the element loses the focus
onchange DHTML: The client-side script method to be called when the element value is changed
onclearThe client-side script method to be called when a file entry is cleared
onclick DHTML: The client-side script method to be called when the element is clicked
ondblclick DHTML: The client-side script method to be called when the element is double-clicked
onerrorThe client-side script method to be called when a file uploading is interrupted according to any errors
onfileuploadcompleteThe client-side script method to be called when a file is uploaded to the server
onfocus DHTML: The client-side script method to be called when the element gets the focus
onkeydown DHTML: The client-side script method to be called when a key is pressed down over the element
onkeypress DHTML: The client-side script method to be called when a key is pressed over the element and released
onkeyup DHTML: The client-side script method to be called when a key is released
onmousedown DHTML: The client-side script method to be called when a mouse button is pressed down over the element
onmousemove DHTML: The client-side script method to be called when a pointer is moved within the element
onmouseout DHTML: The client-side script method to be called when a pointer is moved away from the element
onmouseover DHTML: The client-side script method to be called when a pointer is moved onto the element
onmouseup DHTML: The client-side script method to be called when a mouse button is released
onselect DHTML: The client-side script method to be called when some text is selected in the text field. This attribute can be used with the INPUT and TEXTAREA elements.
onsizerejectedThe client-side script method to be called when a file uploading is rejected by the file size overflow
ontyperejectedThe client-side script method to be called when a file type is rejected according to the file types allowed
onuploadThe client-side script method to be called when a file uploading is started
onuploadcanceledThe client-side script method to be called when a file uploading is cancelled
onuploadcompleteThe client-side script method to be called when uploading of all files from the list is completed
progressLabelDefines a label for a progress label
rendered JSF: If "false", this component is not rendered
required JSF: If "true", this component is checked for non-empty input
requiredMessageA ValueExpression enabled attribute which defines text of validation message to show, if a required field is missing
sizeErrorLabelDefines a label for a size error label
statusID (in format of call UIComponent.findComponent()) of Request status component
stopButtonClassAssigns one or more space-separated CSS class names to the component 'Cancel' button
stopButtonClassDisabledAssigns one or more space-separated CSS class names to the component 'Cancel' button disabled
stopControlLabelDefines a label for a stop button
stopEntryControlLabelDefines a label for a stop control
styleHTML: CSS style rules to be applied to the component
styleClass JSF: Assigns one or more CSS class names to the component. Corresponds to the HTML "class" attribute.
tabindexHTML: This attribute specifies the position of the current element in the tabbing order for the current document. This value must be a number between 0 and 32767. User agents should ignore leading zeros
transferErrorLabelDefines a label for a transfer error label
uploadButtonClassAssigns one or more space-separated CSS class names to the component 'Upload' button
uploadButtonClassDisabledAssigns one or more space-separated CSS class names to the component 'Upload' button disabled
uploadControlLabelDefines a label for an upload button
uploadDataCollection of files uploaded
uploadListClassAssigns one or more space-separated CSS class names to the upload list
uploadListClassDisabledAssigns one or more space-separated CSS class names to the upload list disabled
validator JSF: MethodBinding pointing at a method that is called during Process Validations phase of the request processing lifecycle, to validate the current value of this component
validatorMessageA ValueExpression enabled attribute that, if present, will be used as the text of the validator message, replacing any message that comes from the validator


The <rich:fileUpload> component consists of two parts:

There are two places where the uploaded files are stored:

The "uploadData" attribute defines the collection of files uploaded. See the example below.

Example:


...
<rich:fileUpload uploadData="#{bean.data}"/>
...

The "fileUploadedListener" is called at server side after every file uploaded and used for the saving files from temporary folder or RAM.

Example:


...
<rich:fileUpload uploadData="#{bean.data}" fileUploadListener="#{bean.listener}"/>
 ...

The following methods for processing the uploaded files are available:

Automatically files uploading could be performed by means of the "immediateUpload" attribute. If the value of this attribute is "true" files are uploaded automatically once they have been added into the list. All next files in the list are uploaded automatically one by one. If you cancel uploading process next files aren't started to upload till you press the "Upload" button or clear the list.

Example:


...
<rich:fileUpload uploadData="#{bean.data}" fileUploadListener="#{bean.listener}" immediateUpload="true"/>
 ...

The "autoclear" attribute is used to remove automatically files from the list after upload completed. See the simple example below.

Example:


...
<rich:fileUpload uploadData="#{bean.data}" autoclear="true"/>
...

Each file in list waiting for upload has link "Cancel" opposite its name. Clicking this link invokes JS API remove() function, which gets $('id').component.entries[i] as a parameter and removes the particular file from list and from the queue for upload. After a file has been uploaded the link "Cancel" changes to "Clear". Clicking "Clear" invokes clear() JS API function, which also gets ID of the particular entry and removes it from the list. Uploaded to server file itself is kept untouched.

The <rich:fileUpload> component provides following restrictions:

The <rich:fileUpload> component provides a number of specific event attributes:

The <rich:fileUpload> component has an embedded Flash module that adds extra functionality to the component. The module is enabled with "allowFlash" attribute set to "true".

These are the additional features that the Flash module provides:

Apart from uploading files to the sever without using Ajax, the Flash module provides a number of useful API functions that can be used to obtain information about the uploaded file.

There are 2 ways to obtain the data stored in the FileUploadEntry object.

  • By means of JavaScript on the client side. Use the following syntax for that entries[i].propertyName. For example entries[0].state will return the state of the file the is being processed or has just been processed.

  • The properties of FileUploadEntry object can be retrieved using the entry.propertyName expression in the specific event attributes. For example, onupload="alert(event.memo.entry.fileName);" will display a message with the name of the file at the very moment when upload operation starts. A full list of properties can be found in RichFaces Developer Guide section on properties and their attributes.

The given bellow code sample demonstrates how the properties can be used. Please study it carefully.


...
<head>
        <script>
                function _onaddHandler (e) {
                    var i = 0;
                        for (; i < e.memo.entries.lenghti++) {
                                alert(e.memo.entries[i].creator);  //Shows creators of the added files
                        }
                }

                function _onerrorhandle(e) {
                        alert(e.memo.entry.fileName + "file was not uploaded due  transfer error");
                }
        </script>
</head>
 ...

Moreover, embedded Flash module provides a smoother representation of progress bar during the uploading process: the polling is performed is not by Ajax, but my means of the flash module.


However, the Flash module doens't perform any visual representation of the component.

In order to customize the information regarding the ongoing process you could use "label" facet with the following macrosubstitution:

  • {B}, {KB}, {MB} contains the size of file uploaded in bytes, kilobytes, megabytes respectively

  • {_B}, {_KB}, {_MB} contains the remain file size to upload in bytes, kilobytes, megabytes respectively

  • {ss}, {mm}, {hh} contains elapsed time in seconds, minutes and hours respectively

Example:


...
<rich:fileUpload uploadData="#{bean.data}" fileUploadListener="#{bean.listener}">
        <f:facet name="label">
                <h:outputText value="{_KB}KB from {KB}KB uploaded --- {mm}:{ss}" />
        </f:facet>
</rich:fileUpload>
...

This is the result:


You could define labels of the component controls with the help of "addControlLabel" , "clearAllControlLabel" , "clearControlLabel" , "stopEntryControlLabel" , "uploadControlLabel" attributes. See the following example.

Example:


...
<rich:fileUpload addControlLabel="Add file..." clearAllControlLabel="Clear all" clearControlLabel="Clear"
                        stopEntryControlLabel="Stop process" uploadControlLabel="Upload file"/>
...

This is the result:


The <rich:fileUpload> component allows to use sizes attributes:

  • "listHeight" attribute specify height for list of files in pixels

  • "listWidth" attribute specify width for list of files in pixels

In order to disable the whole component you could use the "disabled" attribute. See the following example.

Example:


...
<rich:fileUpload disabled="true"/>
...

This is the result:


It's possible to handle events for fileUpload using JavaScript code. A simplest example of usage JavaScript API is placed below:

Example:


...
<rich:fileUpload  id="upload" disabled="false"/>
<h:commandButton onclick="${rich:component('upload')}.disable();" value="Disable" />
...

<rich:fileUpload> component also provides a number of JavaScript property, that can be used to process uploaded files, file states etc. The given below example illustrates how the entries[0].state property can be used to get access to the file state. Full list of JavaScript properties can be found below.


...
<rich:fileUpload fileUploadListener="#{fileUploadBean.listener}"
                maxFilesQuantity="#{fileUploadBean.uploadsAvailable}"
                id="upload"
                immediateUpload="#{fileUploadBean.autoUpload}"
                acceptedTypes="jpg, gif, png, bmp"/>
        <a4j:support event="onuploadcomplete" reRender="info"  />
</rich:fileUpload>
<h:commandButton onclick="if($('j_id232:upload').component.entries[0].state == FileUploadEntry.UPLOAD_SUCCESS) alert ('DONE');" value="Check file state"/>
...

The <rich:fileUpload> component allows to use internationalization method to redefine and localize the labels. You could use application resource bundle and define RICH_FILE_UPLOAD_CANCEL_LABEL, RICH_FILE_UPLOAD_STOP_LABEL, RICH_FILE_UPLOAD_ADD_LABEL, RICH_FILE_UPLOAD_UPLOAD_LABEL, RICH_FILE_UPLOAD_CLEAR_LABEL, RICH_FILE_UPLOAD_CLEAR_ALL_LABEL, RICH_FILE_UPLOAD_PROGRESS_LABEL, RICH_FILE_UPLOAD_SIZE_ERROR_LABLE, RICH_FILE_UPLOAD_TRANSFER_ERROR_LABLE, RICH_FILE_UPLOAD_ENTRY_STOP_LABEL, RICH_FILE_UPLOAD_ENTRY_CLEAR_LABEL, RICH_FILE_UPLOAD_ENTRY_CANCEL_LABEL there.

The <rich:fileUpload> component could work together with Seam framework. On RichFaces LiveDemo page you can see how to configure filter for this framework in web.xml file in order to handle <rich:fileUpload> requests.

To make <rich:fileUpload> component work properly with MyFaces extensions, the order in which filters are defined and mapped in web.xml, is important. See corresponding FAQ chapter.


Table 6.407. Client-side object properties

PropertyDescription
entriesReturns a array of all files in the list
entries.lengthReturns the number of files in the list
entries[i].fileNameReturns the file name, that is retrieved by the array index
entries[i].state Returns the file state. Possible states are
  • "initialized" - the file is added, corresponds to FileUploadEntry.INITIALIZED constant

  • "progress" - the file is being uploaded, corresponds to FileUploadEntry.UPLOAD_IN_PROGRESS constant

  • "ready" - uploading is in process, corresponds to FileUploadEntry.READY constant The file will be uploaded on queue order.

  • "canceled" - uploading of the file is canceled, corresponds to FileUploadEntry.UPLOAD_CANCELED constant

  • "done" - the file is uploaded successfully, corresponds to FileUploadEntry.UPLOAD_SUCCESS constant

  • "transfer_error" - a file transfer error occurred, corresponds to FileUploadEntry.UPLOAD_TRANSFER_ERROR constant

  • "size_error" - the file exceeded maximum size, corresponds to FileUploadEntry.UPLOAD_SIZE_ERROR constant

entries[i].size Returns the size of the file. Available in flash enabled version only
entries[i].TypeReturns the mime type of the file. Available in flash enabled version only
entries[i].creator Returns the name of the author of the file. Available in flash enabled version only
entries[i].creationDateReturns the date when the file was created. Available in flash enabled version only
entries[i].modificationDateReturns the date of the last file modification. Available in flash enabled version only

Table 6.408. Client-side object properties available with specific event attributes

PropertyDescription
entry.state Returns the file state. Possible states are
  • "initialized" - the file is added, corresponds to FileUploadEntry.INITIALIZED constant

  • "progress" - the file is being uploaded, corresponds to FileUploadEntry.UPLOAD_IN_PROGRESS constant

  • "ready" - uploading is in process, corresponds to FileUploadEntry.READY constant The file will be uploaded on queue order.

  • "canceled" - uploading of the file is canceled, corresponds to FileUploadEntry.UPLOAD_CANCELED constant

  • "done" - the file is uploaded successfully, corresponds to FileUploadEntry.UPLOAD_SUCCESS constant

  • "transfer_error" - a file transfer error occurred, corresponds to FileUploadEntry.UPLOAD_TRANSFER_ERROR constant

  • "size_error" - the file exceeded maximum size, corresponds to FileUploadEntry.UPLOAD_SIZE_ERROR constant

entry.fileName Returns the file's name. This property works with all event handlers except for "onadd".
entry.size Returns the size of the file. Available in flash enabled version only
entry.TypeReturns the mime type of the file. Available in flash enabled version only
entry.creator Returns the name of the author of the file. Available in flash enabled version only
entry.creationDateReturns the date when the file was created. Available in flash enabled version only
entry.modificationDateReturns the date of the last file modification. Available in flash enabled version only













The following picture illustrates how CSS classes define styles for component elements.







In order to redefine styles for all <rich:fileUpload> components on a page using CSS, it's enough to create classes with the same names (possible classes could be found in the tables above) and define necessary properties in them.

Example:


...
.rich-fileupload-anc{
    
font-weight:bold;
    
text-decoration:none;
}
...

This is the result:


In the example above the font weight and text decoration for "Cancel" and "Clear" links are changed.

Also it's possible to change styles of particular <rich:fileUpload> component. In this case you should create own style classes and use them in the corresponding <rich:fileUpload> styleClass attributes. An example is placed below:

Example:


...
.myClass{
        
font-weight:bold;
}
...

The "addButtonClass" attribute for <rich:fileUpload> is defined as it's shown in the example below:

Example:


<rich:fileUpload ... addButtonClass="myClass"/>

This is the result:


As it could be seen on the picture above, the font style for "Add" button is changed.

On RichFaces LiveDemo page you can see an example of <rich:fileUpload> usage and sources for the given example.