6.50.  < rich:fileUpload >

6.50.1. Description

The <rich:fileUpload> component designed to perform Ajax-ed files upload to server.

<rich:fileUpload> component

Figure 6.90.  <rich:fileUpload> component


6.50.2. Key Features

  • ProgressBar shows the status of downloads

  • File types, file sizes and files count restrictions

  • Multiple files upload support

  • Possibility to cancel the request

  • One request for every upload

  • Automatic uploads

  • Supports standard JSF internationalization

  • Highly customizable look and feel

  • Disablement support

Table 6.191. rich : fileUpload attributes

Attribute NameDescription
acceptedTypesFiles types allowed to upload
accesskeyThis 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
addButtonClassCSS style for add button
addButtonClassDisabledCSS style for add button disabled
addControlLabelDefines a label for an add button
ajaxSingleIf this attribute set up in true upload request will be like as single. No components value in form will be updated after file uploaded.
alignleft|center|right|justify [CI] Deprecated. This attribute specifies the horizontal alignment of its element with respect to the surrounding context. Possible values: * left: text lines are rendered flush left. * center: text lines are centered. * right: text lines are rendered flush right. * justify: text lines are justified to both margins. The default depends on the base text direction. For left to right text, the default is align=left, while for right to left text, the default is align=right
altFor 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 immediatelly removed from list after upload completed
bindingThe attribute takes a value-binding expression for a component property of a backing bean
cancelButtonClassCSS style for cancel button
cancelButtonClassDisabledCSS style for cancel button disabled
cancelEntryControlLabelDefines a label for a cancel control
cleanButtonClassCSS style for clean button
cleanButtonClassDisabledCSS style for clean button disabled
clearAllControlLabelDefines a label for a clearAll button
clearControlLabelDefines a label for a clear control
converterId of Converter to be used or reference to a Converter
converterMessageA ValueExpression enabled attribute that, if present, will be used as the text of the converter message, replacing any message that comes from the converter
disabledAttribute 'disabled' provides possibility to make the whole component disabled if its value equals to "true".
fileEntryClassCSS style upload file entry
fileEntryClassDisabledCSS style upload file entry disabled
fileEntryControlClassCSS style for upload enrty control
fileEntryControlClassDisabledCSS style for upload enrty control disabled
fileUploadListenerMethodExpression representing an action listener method that will be notified after file uploaded.
idEvery 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 immediatelly uploaded after they have been added in list
listHeightDefines height of file list
listWidthDefines width of file list
localeUsed for locale definition
maxFilesQuantityDefines max files count allowed for upload (optional).
noDuplicateDefines if component should allow to add files that was already in list
onblurHTML: script expression; the element lost the focus
onchangeHTML: script expression; the element value was changed
onclearA JavaScript event handler called when the file entries were cleared
onclickHTML: a script expression; a pointer button is clicked
ondblclickHTML: a script expression; a pointer button is double-clicked
onerrorA JavaScript event handler called when the file upload was interrupted according to any errors
onfocusHTML: script expression; the element got the focus
onkeydownHTML: a script expression; a key is pressed down
onkeypressHTML: a script expression; a key is pressed and released
onkeyupHTML: a script expression; a key is released
onmousedownHTML: script expression; a pointer button is pressed down
onmousemoveHTML: a script expression; a pointer is moved within
onmouseoutHTML: a script expression; a pointer is moved away
onmouseoverHTML: a script expression; a pointer is moved onto
onmouseupHTML: script expression; a pointer button is released
onselectHTML: script expression; The onselect event occurs when you select some text in a text field. This attribute may be used with the INPUT and TEXTAREA elements
onsizerejectedA JavaScript event handler called when the file uploading was rejected by fiel size overflow
ontyperejectedA JavaScript event handler called when the file type was rejected according to file types allowed
onuploadA JavaScript event handler called on an upload operation
onuploadcanceledA JavaScript event handler called when upload is cancelled
onuploadcompleteA JavaScript event handler called when upload is completed
renderedIf "false", this component is not rendered
requiredIf "true", this component is checked for non-empty input
requiredMessageA ValueExpression enabled attribute that, if present, will be used as the text of the validation message for the "required" facility, if the "required" facility is used
stopControlLabelDefines a label for a stop button
stopEntryControlLabelDefines a label for a stop control
styleCSS style(s) is/are to be applied when this component is rendered
styleClassCorresponds to the HTML class attribute
tabindexThis 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
uploadButtonClassCSS style for upload button
uploadButtonClassDisabledCSS style for upload button disabled
uploadControlLabelDefines a label for an upload button
uploadDataCollection of files uploaded
uploadListClassCSS style for upload list
uploadListClassDisabledCSS style for upload list disabled
validatorMethodBinding 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
valueChangeListenerListener for value changes

Table 6.192. Component identification parameters

NameValue
component-typeorg.richfaces.component.FileUpload
component-classorg.richfaces.component.html.HtmlFileUpload
component-familyorg.richfaces.component.FileUpload
renderer-typeorg.richfaces.renderkit.html.FileUploadRenderer
tag-classorg.richfaces.taglib.FileUploadTag

6.50.3. Creating the Component with a Page Tag

To create the simplest variant on a page use the following syntax:

Example:


...
<rich:fileUpload /> 
...

6.50.4. Creating the Component Dynamically Using Java

Example:


import org.richfaces.component.html.HtmlFileUpload;
... 
HtmlFileUpload myFileUpload = new HtmlFileUpload();
...

6.50.5. Details of Usage

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

  • List of files which contains the list of currently chosen files to upload with possibility to manage every file

  • Component controls - the bar with controls for managing the whole component

There are two places where uploaded files are stored:

  • In the temporary folder (depends on OS) if the value of the createTempFile parameter in Ajax4jsf Filter section is "true" (by Default)

    
    ...
    <init-param>
            <param-name>createTempFiles</param-name>
            <param-value>true</param-value>
    </init-param>
    ...
  • In the RAM if the value of the createTempFile parameter in Ajax4jsf Filter section is "false". This is a better way for storing small-sized files.

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}"/>
 ...

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"/>
...

The <rich:fileUpload> component provides following restrictions:

  • By file types, use "acceptedTypes" attribute to define file types accepted by component. In the example below only files with "html" and "jpg" extensions are accepted to upload.

    Example:

    
    ...
    <rich:fileUpload acceptedTypes="html, jpg"/>
    ...
  • By file size, use the maxRequestSize parameter(value in bytes) inside Ajax4jsf Filter section in web.xml:

    
    ...
    <init-param>
            <param-name>maxRequestSize</param-name>
            <param-value>1000000</param-value>
    </init-param>
    ...
  • By max files quantity, use the "maxFilesQuantity" attribute to define max number of files allowed to be uploaded. After a number of files in the list equals to the value of this attribute "Add" button is disabled and nothing could be uploaded even if you clear the whole list. In order to upload files again you should rerender the component. As it could be seen in the example below, only 2 files are accepted for uploading.

    Example:

    
    ...
    <rich:fileUpload maxFilesQuantity="2"/>
     ...

    This is the result:

    <rich:fileUpload> with "maxFilesQuantity" attribute

    Figure 6.91.  <rich:fileUpload> with "maxFilesQuantity" attribute


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

  • The "onupload" which gives you a possibility to cancel the upload at client side

  • The "onuploadcomplete" which is called after all files from the list are uploaded

  • The "onuploadcanceled" which is called after upload has been canceled via cancel control

  • The "onerror" which is called if the file upload was interrupted according to any errors

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:

<rich:fileUpload> with "label" facet

Figure 6.92.  <rich:fileUpload> with "label" facet


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:

<rich:fileUpload> with labels

Figure 6.93.  <rich:fileUpload> with labels


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:

<rich:fileUpload> with "disabled" attribute

Figure 6.94.  <rich:fileUpload> with "disabled" attribute


It's possible to handle events for fileUpload from 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(event); return false;" value="Disable" />
...

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. Here you can see how to configure Seam Filter in order to handle <rich:fileUpload> requests.

6.50.6. JavaScript API

Table 6.193. JavaScript API

FunctionDescription
enable()Enables the component
disable()Disables the component
stop()Stops the uploading process
clear()Clears list of files

6.50.7. Look-and-Feel Customization

For skinnability implementation, the components use a style class redefinition method. Default style classes are mapped on skin parameters.

There are two ways to redefine the appearance of all <rich:fileUpload> components at once:

  • Redefine the corresponding skin parameters

  • Add to your style sheets style classes used by a <rich:fileUpload> component

6.50.8. Skin Parameters Redefinition

Table 6.194. Skin parameters redefinition for a component

Skin parametersCSS properties
tableBackgroundColorbackground-color
tableBorderColorborder-color

Table 6.195. Skin parameters redefinition for a font

Skin parametersCSS properties
generalFamilyFontfont-family
generalSizeFontfont-size

Table 6.196. Skin parameters redefinition for a toolbar

Skin parametersCSS properties
additionalBackgroundColorbackground-color
tableBorderColorborder-bottom-color
tableBackgroundColorborder-top-color
tableBackgroundColorborder-left-color

Table 6.197. Skin parameters redefinition for items in the list

Skin parametersCSS properties
tableBorderColorborder-bottom-color

Table 6.198. Skin parameters redefinition for a "Cancel", "Clear" links

Skin parametersCSS properties
generalLinkColorcolor

Table 6.199. Skin parameters redefinition for a button

Skin parametersCSS properties
trimColorbackground-color

Table 6.200. Skin parameters redefinition for a button border

Skin parametersCSS properties
tableBorderColorborder-color

Table 6.201. Skin parameters redefinition for a highlighted button

Skin parametersCSS properties
trimColorbackground-color
selectControlColorborder-color

Table 6.202. Skin parameters redefinition for a pressed button

Skin parametersCSS properties
selectControlColorborder-color
additionalBackgroundColorbackground-color

Table 6.203. Skin parameters redefinition for "Upload", "Clean" buttons

Skin parametersCSS properties
generalTextColorcolor

Table 6.204. Skin parameters redefinition for a disabled "Start" button icon

Skin parametersCSS properties
tableBorderColorcolor

Table 6.205. Skin parameters redefinition for a disabled "Clear" button icon

Skin parametersCSS properties
tableBorderColorcolor

6.50.9. Definition of Custom Style Classes

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

Classes names

Figure 6.95. Classes names


Classes names

Figure 6.96. Classes names


Table 6.206. Classes names that define a component representation

Class nameDescription
rich-fileupload-list-decorDefines styles for a wrapper <div> element of a fileUpload
rich-fileupload-fontDefines styles for a font of buttons and items
rich-fileupload-toolbar-decorDefines styles for a toolbar
rich-fileupload-list-overflowDefines styles for a list of files

Table 6.207. Classes names that define buttons representation

Class nameDescription
rich-fileupload-buttonDefines styles for a buttons
rich-fileupload-button-borderDefines styles for a border of buttons
rich-fileupload-button-lightDefines styles for a highlight of button
rich-fileupload-button-pressDefines styles for a pressed button
rich-fileupload-button-disDefines styles for a disabled button
rich-fileupload-button-selectionDefines styles for "Upload", "Clean" buttons

Table 6.208. Classes names that define the representation of the buttons' icons

Class nameDescription
rich-fileupload-icoDefines styles for an icon
rich-fileupload-ico-addDefines styles for a "Add" button icon
rich-fileupload-ico-startDefines styles for a "Upload" button icon
rich-fileupload-ico-stopDefines styles for a "Stop" button icon
rich-fileupload-ico-clearDefines styles for a "Clear" button icon
rich-fileupload-ico-add-disDefines styles for a disabled "Add" button icon
rich-fileupload-ico-start-disDefines styles for a disabled "Upload" button icon
rich-fileupload-ico-clear-disDefines styles for a disabled "Clear" button icon

Table 6.209. Classes names that define list items representation

Class nameDescription
rich-fileupload-table-tdDefines styles for a wrapper <td> element of a list items
rich-fileupload-ancDefines styles for "Cancel", "Stop", "Clear" links

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:

Redefinition styles with predefined classes

Figure 6.97. Redefinition styles with predefined classes


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:

Redefinition styles with own classes and styleClass attributes

Figure 6.98. Redefinition styles with own classes and styleClass attributes


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

6.50.10. Relevant Resources Links

Here you can see an example of <rich:fileUpload> usage and sources for the given example.