File Upload"

From Documentation
(Created page with '{{ZKDevelopersGuidePageHeader}} The file upload dialog is no longer applicable. Rather, you shall use <javadoc>org.zkoss.zul.Button</javadoc> or <javadoc>org.zkoss.zul.Toolbarbu…')
 
m ((via JWB))
 
(9 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{ZKDevelopersGuidePageHeader}}
+
{{ZKDevelopersReferencePageHeader}}
  
The file upload dialog is no longer applicable. Rather, you shall use <javadoc>org.zkoss.zul.Button</javadoc> or <javadoc>org.zkoss.zul.Toolbarbutton</javadoc> with upload="true" instead. For example,
+
= File Upload with Servlet Thread =
 +
 
 +
When the event thread is disable (default), it is recommended to use <javadoc>org.zkoss.zul.Button</javadoc>, <javadoc>org.zkoss.zul.Toolbarbutton</javadoc>  or <javadoc>org.zkoss.zul.Menuitem</javadoc> with upload="true" instead. For example,
  
 
<source lang="xml">
 
<source lang="xml">
 
<zk>
 
<zk>
 
<zscript>
 
<zscript>
void upload(Event event) {
+
void upload(UploadEvent event) {
 
org.zkoss.util.media.Media media = event.getMedia();
 
org.zkoss.util.media.Media media = event.getMedia();
 
if (media instanceof org.zkoss.image.Image) {
 
if (media instanceof org.zkoss.image.Image) {
 
org.zkoss.zul.Image image = new org.zkoss.zul.Image();
 
org.zkoss.zul.Image image = new org.zkoss.zul.Image();
image.setContent(media);
+
image.setContent( (org.zkoss.image.Image) media);
 
image.setParent(pics);
 
image.setParent(pics);
 
} else {
 
} else {
 
Messagebox.show("Not an image: "+media, "Error", Messagebox.OK, Messagebox.ERROR);
 
Messagebox.show("Not an image: "+media, "Error", Messagebox.OK, Messagebox.ERROR);
break; //not to show too many errors
 
 
}
 
}
 
}
 
}
Line 24: Line 25:
 
</source>
 
</source>
  
If you prefer to use a dialog (<javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>), please take a look at [[ZK_Component_Reference/Essential_Components/Fileupload | ZK Component Reference: Fileupload]] for more inormation.
+
If you prefer to use a dialog (<javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>), please take a look at [[ZK_Component_Reference/Essential_Components/Fileupload#Event_Thread_Disabled | ZK Component Reference: Fileupload]] for more inormation.
{{ZKDevelopersGuidePageFooter}}
+
 
 +
== File Upload with Event Thread ==
 +
 
 +
If the event thread is disabled, the developer can use <javadoc>org.zkoss.zul.Button</javadoc> or <javadoc>org.zkoss.zul.Toolbarbutton</javadoc> 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 <javadoc method="get()">org.zkoss.zul.Fileupload</javadoc> and other static methods.
 +
 
 +
<source lang="xml">
 +
<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>
 +
</source>
 +
 
 +
As shown, <javadoc method="get(int)">org.zkoss.zul.Fileupload</javadoc> won't return until the end user uploads the files (and/or closes the dialog).
 +
 
 +
=Version History=
 +
{{LastUpdated}}
 +
{| class='wikitable' | width="100%"
 +
! Version !! Date !! Content
 +
|-
 +
| &nbsp;
 +
| &nbsp;
 +
| &nbsp;
 +
|}
 +
 
 +
{{ZKDevelopersReferencePageFooter}}:

Latest revision as of 02:59, 12 January 2022

File Upload with Servlet Thread

When the event thread is disable (default), it is recommended to use Button, Toolbarbutton or Menuitem 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( (org.zkoss.image.Image) media);
			image.setParent(pics);
		} else {
			Messagebox.show("Not an image: "+media, "Error", Messagebox.OK, Messagebox.ERROR);
		}
	}
	</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 : 2022/01/12


Version Date Content
     


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