30.23. <w:fileUploadProgress>

FileUploadProgress是展示文件上传进度的构件,需与Fileupload构件一起使用。由于FileUploadProgress构件具有默认行为,可自动显示当前文件上传状态,简单易用。

30.23.1. 构件信息

表 30.66. 构件信息

Component Typeorg.operamasks.faces.component.widget.UIFileUploadProgress
Component Familyorg.operamasks.faces.widget.FileUploadProgress
Component Classorg.operamasks.faces.component.widget.UIFileUploadProgress
Tag Classorg.operamasks.faces.webapp.widget.UIFileUploadProgressTag
Renderer Typeorg.operamasks.faces.component.widget.UIFileUploadProgress
Renderer Class(AJAX)org.operamasks.faces.render.widget.ajax.AjaxFileUploadProgressRenderer

继承体系

+java.lang.Object

++javax.faces.component.UIComponent

+++javax.faces.component.UIComponentBase

++++org.operamasks.faces.component.widget.base.UIFileUploadProgressBase

+++++org.operamasks.faces.component.widget.UIFileUploadProgress

30.23.2. 属性

表 30.67. 属性

binding 定义类 javax.faces.component.UIComponentBase
类型 javax.el.ValueExpression(javax.faces.component.UIComponent)
是否必须 延时求值
一个值表达式,用于把该组件链接到一个ManagedBean的某个属性。
completeMessage 定义类 org.operamasks.faces.component.widget.UIFileUploadProgress
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
文件上传结束时构件显示的信息
errorMessage 定义类 org.operamasks.faces.component.widget.UIFileUploadProgress
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
文件上传出错时构件显示的错误信息
id 定义类 javax.faces.component.UIComponentBase
类型 java.lang.String
是否必须 延时求值
这个组件的组件标识符。这个值在最近的命名容器类型的父组件范围内,必须是唯一的。
interval 定义类 org.operamasks.faces.component.widget.base.UIFileUploadProgressBase
类型 javax.el.ValueExpression(java.lang.Integer)
是否必须 延时求值
在上传过程中,每隔多少秒构件刷新一次
rendered 定义类 javax.faces.component.UIComponentBase
类型 boolean(boolean)
是否必须 延时求值
一个标志,指出该组件是否要在任何随后的form提交过程中被渲染或处理。 这个属性的缺省值是true。
startMessage 定义类 org.operamasks.faces.component.widget.UIFileUploadProgress
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
文件开始上传时构件显示的信息
uploadingMessage 定义类 org.operamasks.faces.component.widget.UIFileUploadProgress
类型 javax.el.ValueExpression(java.lang.String)
是否必须 延时求值
文件上传过程中构件显示的信息

30.23.3. 构件API

30.23.4. 示例

下面是FileUploadProgress构件的一般用法,此示例来自 bpdemos

首先创建托管Bean:

@ManagedBean(name = "interact.fileUploadBean", scope = ManagedBeanScope.REQUEST)
public class FileUploadBean {
    private UIFileUpload fileUpload1;
    private String errorMessage;

    public void processUpload(FileUploadItem fileUploadItem) {
        if (fileUploadItem.getFieldName().equals("fileUpload1"))
            deleteOldFiles();

        InputStream input = null;
        FileOutputStream output = null;

        try {
            input = fileUploadItem.openStream();
            output = new FileOutputStream(new File(getSaveToPath(fileUploadItem)));
            byte[] buf = new byte[4096];
            // UIFileUpload.END_UPLOADING为-1,这里表示数据输入流已经读取完毕
            int length = UIFileUpload.END_UPLOADING;

            while ((length = input.read(buf)) != UIFileUpload.END_UPLOADING) {
                output.write(buf, 0, length);
            }
        } catch (IOException e) {
            throw new FacesException(e);
        } finally {
            if (output != null)
                try {
                    output.close();
                } catch (IOException e) {
                }
        }
    }

    ...... 

}

下面是页面代码:

<w:fileUpload id="fileUpload1" uploadListener="#{interact.fileUploadBean.processUpload}"
    binding="#{interact.fileUploadBean.fileUpload1}" rich="true" maxSize="100k"
    required="true" />
<br />
<w:fileUpload id="fileUpload2" uploadListener="#{interact.fileUploadBean.processUpload}"
    rich="false" maxSize="100k" />
<br />
<w:fileUpload id="fileUpload3" writeTo="#{interact.fileUploadBean.fileUpload3WriteTo}"
    rich="true" browseIcon="images/select.gif" maxSize="100k" />
<br />
<w:button action="#{interact.fileUploadBean.action}" value="Upload" />
<w:fileUploadProgress startMessage="开始上传"
    uploadingMessage="正在上传,已经上传{readBytes}k/{total}k({percentage}%)"
    completeMessage="上传结束,总共{total}k" errorMessage="上传错误:{error}" />