UploadAgent"

From Documentation
Line 59: Line 59:
 
<references/>
 
<references/>
  
= Upload Files with Fileupload.get() =
+
= Uploading Files with Fileupload.get() =
  
Another way to upload files is to invoke the static method <javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>.<ref>for more detail, please refer to [[ZK Component Reference/Essential Components/Fileupload#Invoke the Static Method: get]]</ref> This static method will open a uploading dialog and allow users to upload single or multiple files (if configured), as following image shows.
+
Another way to upload files is to invoke the static method <javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>.<ref>For more detail, please refer to [[ZK Component Reference/Essential Components/Fileupload#Invoke the Static Method: get]]</ref> This static method will open up am uploading dialog and allow users to upload single or multiple files (if configured), as shown in the image below.
  
 
[[File:Zats upload dialog.png]]
 
[[File:Zats upload dialog.png]]
  
In this kind of case, we can retrieve <tt>UploadAgent</tt> from casting <javadoc>org.zkoss.zk.ui.DesktopAgent</javadoc>. Following is a typical example of file uploading with <javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>:
+
In this case, we can retrieve <tt>UploadAgent</tt> from casting <javadoc>org.zkoss.zk.ui.DesktopAgent</javadoc>. Following is a typical example of file uploading with <javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>:
  
 
<source lang="java" start="10" high="14, 15, 16, 17, 18">
 
<source lang="java" start="10" high="14, 15, 16, 17, 18">
Line 79: Line 79:
 
}
 
}
 
</source>
 
</source>
* '''Line 14-15''': After triggering an event led to upload operation, we can cast <javadoc>org.zkoss.zk.ui.DesktopAgent</javadoc> as a <tt>UploadAgent</tt> for uploading.
+
* '''Line 14-15''': After triggering an event leading to an uploading operation, we can cast <javadoc>org.zkoss.zk.ui.DesktopAgent</javadoc> as a <tt>UploadAgent</tt> for uploading.
* '''Line 16-17''': We can upload multiple files at once when using <javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>.
+
* '''Line 16-17''': We can also upload multiple files at once using <javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>.
 
* '''Line 18''': Don't forget to invoke <tt>finish()</tt>method.
 
* '''Line 18''': Don't forget to invoke <tt>finish()</tt>method.
  

Revision as of 10:41, 2 July 2012

WarningTriangle-32x32.png This page is under construction, so we cannot guarantee the accuracy of the content!


Since 1.1.0

ZATS Mimic introduces the UploadAgent to simulate file uploading operation with consistent usage. UploadAgent supports single or multiple file uploading like ZK components. Following are the implementation steps:

  1. Obtain a UploadAgent instance according to your case. Note that you should use the same instances in one uploading iteration.
  2. Upload a file by invoking upload() method.
  3. Invoke finish()method when the uploading is done.

Uploading Files

To set up an uploading feature, you can simply use the Fileupload component. Alternatively, you can choose to use any button components such as Button, Menuitem, Toolbarbutton and so on.[1] and simply assign the upload attribute to these components; users can then click and select a file to upload through a browser dialog, as illustrated below:

Zats upload button.png

Notes

Upload A Single File

We can cast these components described above as UploadAgent and perform file uploading. Following is a typical example of a single file uploading:

@Test
public void test() throws Exception {
	File file = getFile();
	DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
	UploadAgent agent = desktop.query("#btn").as(UploadAgent.class);
	agent.upload(file, "text/plain");
	agent.finish();
}
  • Line 14: Cast component to UploadAgent and keep its reference.
  • Line 15: Invoke upload() method to upload a file.
  • Line 16: Don't forget to invoke finish()method.

Uploading Multiple Files

Since ZK 6.0.0, components now also support mutiple uploads at once if they have multiple=true flag and users using web browsers supporting HTML5.[1] UploadAgent also supports multiple files uploading at once. Following is a typical example of uploading multiple files:

@Test
public void test() throws Exception {
	File[] files = getFiles();
	DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
	UploadAgent agent = desktop.query("#btn").as(UploadAgent.class);
	agent.upload(files[0], "text/plain");
	agent.upload(files[1], "image/png");
	agent.finish();
}
  • Line 14: Cast component to UploadAgent and keep its reference.
  • Line 15-16: We can upload multiple files at once with ZK version greater than 6.0.0 and components that have multiple=true flag.
  • Line 17: Don't forget to invoke finish()method.


Notes

Uploading Files with Fileupload.get()

Another way to upload files is to invoke the static method Fileupload.get().[1] This static method will open up am uploading dialog and allow users to upload single or multiple files (if configured), as shown in the image below.

Zats upload dialog.png

In this case, we can retrieve UploadAgent from casting DesktopAgent. Following is a typical example of file uploading with Fileupload.get():

@Test
public void test() throws Exception {
	File[] files = getFiles();
	DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
	desktop.query("#label2").click();
	UploadAgent agent = desktop.as(UploadAgent.class);
	agent.upload(files[0], "text/plain");
	agent.upload(files[1], "image/png");
	agent.finish();
}
  • Line 14-15: After triggering an event leading to an uploading operation, we can cast DesktopAgent as a UploadAgent for uploading.
  • Line 16-17: We can also upload multiple files at once using Fileupload.get().
  • Line 18: Don't forget to invoke finish()method.

Notes

Supported Components

Components
Version
Note
DesktopAgent 5, 6
Fileupload 5, 6
Button 5, 6
Menuitem 5, 6
Toolbarbutton 5, 6




Last Update : 2012/07/02

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