Cmobilecom AF 5.19 Developer Guide

14 File Upload

If files are uploaded as attachments of an entity, see Large Object and Media.

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.

Open Upload Form

Another way to upload files is to open a separate upload form with a UploadListener. The listener will be called after files are uploaded. For example,


	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.

Ajax and Progress

By default, files will be uploaded by ajax if HTML5 is supported by browser. Otherwise it will fall back to non-ajax POST.

In case of ajax upload, uploading progress will be shown including loaded, total, speed and time left if HTML5 progress is supported by browser.

Drag and Drop

Files can be selected, or dragged from File explorer and dropped into the marked droppable area. A list of files to be uploaded are listed with thumbnails for images.

Count, Size and Types

By default, the number of files, the size of each file and file types are unlimited.

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");
Large Object and MediaConversionFrames / No Frames