禁用"选择"在primefaces中按钮fileUpload

时间:2014-10-13 16:52:17

标签: jsf file-upload jsf-2 primefaces

我将文件限制设置为1。 我希望当用户选择第一个文件时,选择按钮被禁用。

2 个答案:

答案 0 :(得分:1)

我决定在我的bean中使用boolean变量将一个禁用的atribut置于fileUpload中。 当文件完成上传时,变量将其值更改为true并更新fileUpload componnt,然后组件被禁用。 谢谢大家的帮助。

答案 1 :(得分:0)

试试这个。

XHTML

<p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" 
              mode="advanced" 
              dragDropSupport="false" 
              update="messages,@this" 
              sizeLimit="100000" 
              fileLimit="1" 
              allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
              disabled="#{fileUploadView.fileUploadCount >= 1}"/>

<p:growl id="messages" showDetail="true" />   

managedbean

@ManagedBean
public class FileUploadView {

    private int fileUploadCount;

    public int getFileUploadCount() {
        return fileUploadCount;
    }

    public void setFileUploadCount(int fileUploadCount) {
        this.fileUploadCount = fileUploadCount;
    }

    public void handleFileUpload(FileUploadEvent event) {
        FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);

        fileUploadCount = fileUploadCount + 1;
    }
}

这提议很容易改变文件限制。如果要将fileLimit更改为2,只需更改即可 fileLimit="2"disabled="#{fileUploadView.fileUploadCount >= 2}"没有修改managedbean。