File Upload

From Documentation
Revision as of 08:05, 17 December 2010 by Char (talk | contribs)

File Upload with Servlet Thread

When the event thread is disable (default), it is recommended to use Button or Toolbarbutton with upload="true" instead. For example,

<zk>
	<zscript>
	void upload(UploadEvent event) {
		org.zkoss.util.media.Media media = event.getMedia();
		if (media instanceof org.zkoss.image.Image) {
			org.zkoss.zul.Image image = new org.zkoss.zul.Image();
			image.setContent(media);
			image.setParent(pics);
		} else {
			Messagebox.show("Not an image: "+media, "Error", Messagebox.OK, Messagebox.ERROR);
			break; //not to show too many errors
		}
	}
	</zscript>
	<button label="Upload" upload="true" onUpload="upload(event)"/>
	<toolbarbutton label="Upload" upload="true" onUpload="upload(event)"/>
	<vbox id="pics" />
</zk>

If you prefer to use a dialog (Fileupload.get()), please take a look at ZK Component Reference: Fileupload for more inormation.

File Upload with Event Thread

If the event thread is disabled, the developer can use Button or Toolbarbutton with upload="true" instead. They behaves the same no matter the event thread is disabled or not.

However, if the event thread is disabled, it is convenient to use Fileupload.get() and other static methods.

<zk>
	<button label="Upload">
	<attribute name="onClick">{
		org.zkoss.util.media.Media[] media = Fileupload.get(-1);
		if (media != null) {
			for (int i = 0; i &lt; media.length; i++) {
				if (media[i] instanceof org.zkoss.image.Image) {
					org.zkoss.zul.Image image = new org.zkoss.zul.Image();
					image.setContent(media[i]);
					image.setParent(pics);
				} else {
					Messagebox.show("Not an image: "+media[i], "Error", Messagebox.OK, Messagebox.ERROR);
					break; //not to show too many errors
				}
			}
		}
	}</attribute>
	</button>
	<vbox id="pics" />
</zk>

As shown, Fileupload.get(int) won't return until the end user uploads the files (and/or closes the dialog).

Version History

Last Update : 2010/12/17


Version Date Content
     



Last Update : 2010/12/17

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.