file-upload – 如何限制允许上传的文件数量

如何在JSF 2 primefaces应用程序中限制允许上传的文件数量.属性’filelimit’在primeface 3.3中有,但我使用的是primefaces 3.4
最佳答案
看起来在Issue 3618中删除了fileLimit选项

如注释中所述,在backing bean中使用计数器并丢弃额外的文件或添加FacesMessage以通知用户已达到最大fileLimit.

代码示例:

@ManagedBean
@ViewScoped
public class FileUploadController {

    private final static int MAX_NUM_FILES = 3;
    private List<UploadedFile> uploadedFiles;
    private int counter = 0;

    @PostConstruct
    public void init() {
        uploadedFiles = new ArrayList<UploadedFile>();
    }


    public void handleFileUpload(FileUploadEvent event) {
        if (counter < MAX_NUM_FILES) {
            uploadedFiles.add(event.getFile());
            FacesMessage msg = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
            counter ++;       
        }
        else {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "File Upload Limit Reached ", event.getFile().getFileName() + " is not uploaded.");
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }

    }
}

转载注明原文:file-upload – 如何限制允许上传的文件数量 - 代码日志