如何限制在JSF 2 + primefaces应用程序中允许使用的文件上传数量。属性'filelimit'在primeface 3.3中存在,但我使用的是primefaces 3.4
最佳答案
看起来在Issue 3618中已删除fileLimit选项
如注释中所述,在后备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);
}
}
}