Reputation: 1648
How to limit number of files allowed to be uploaded using in JSF 2 + primefaces application. Attribute 'filelimit' was there in primeface 3.3 but I am using primefaces 3.4
Upvotes: 3
Views: 2079
Reputation: 8574
Looks like the fileLimit option has been removed in Issue 3618
As mentioned in the comments use a counter in the backing bean and either discard extra files or add a FacesMessage to notify the user that maximum fileLimit has been reached.
Code sample:
@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);
}
}
}
Upvotes: 10