In general, to enable a file upload when creating an entity, add a property whose type is UploadedFile or a list of UploadedFile(s). For example,
public class MyType extends PersistenceEntityBase {
...
private List<UploadedFile> photos;
@Transient
public List<UploadedFile> getPhotos() {
return photos;
}
public void setPhotos(List<UploadedFile> photos) {
this.photos = photos;
}
}
Annotate the file upload property as following in its EntityBackingBean:
public class MyTypeBean extends EntityBackingBean<MyType> {
@Properties({
...
@Property(name=photos,
entityPropertyType=EntityFileUploadProperty.class,
renderStyle=@RenderStyleDef(style=RenderStyle.FILE_UPLOAD))
})
public boolean hasPropertyAnnotations() {
return true;
}
}
The uploaded files can be processed in action handler clickMenuNode(),
validate(), preCreate() or postCreate() of its EntityBackingBean.
For example, stream them into database CLOB or BLOB columns, or parse them to create
other entities.
UploadListener listener = new UploadListener() {
@Override
public void filesUploaded(List<UploadedFile> uploadedFiles) throws IOException, SystemException {
// do something
}
};
FileUploadBean.showUploadForm(title, listener, containerRenderRegions);
The parameter containerRenderRegions specifies where to show the upload
form. For example, show it in a region of a dialog or current containerBean.
In case of ajax upload, uploading progress will be shown including loaded, total, speed and time left if HTML5 progress is supported by browser.
To limit file upload size globally, user can set the upload maximum file size in system Parameters.(System -> Settings -> Parameters).
To limit file upload size, count and file types programmatically, set them in EntityFileUploadProperty instance. For example,
EntityFileUploadProperty property = (EntityFileUploadProperty)getEntityProperty("attachments");
property.setCountLimit(10);
property.setSizeLimit(50*1024); // 50K
property.setAcceptTypes("images/*,text/xml,.zip");