ZATS UploadAgent"

From Documentation
Line 6: Line 6:
 
ZATS Mimic introduces the <tt>UploadAgent</tt> to simulate file uploading operation with consistent usage. <tt>UploadAgent</tt> supports single or multiple files uploading as ZK components do.
 
ZATS Mimic introduces the <tt>UploadAgent</tt> to simulate file uploading operation with consistent usage. <tt>UploadAgent</tt> supports single or multiple files uploading as ZK components do.
  
= Upload Files with a Component =
+
= Upload File with a Component =
  
 
The basic way to upload files is using a component such as <javadoc>org.zkoss.zul.Fileupload</javadoc>, <javadoc>org.zkoss.zul.Button</javadoc>, <javadoc>org.zkoss.zul.Menuitem</javadoc>, <javadoc>org.zkoss.zul.Toolbarbutton</javadoc> and so on. <ref>for more detail, please refer to [[ZK Developer's Reference/UI Patterns/File Upload and Download]] and [[ZK Component Reference/Essential Components/Fileupload]]</ref> If we assign the <tt>upload</tt> attribute to these components, users can click and select a file to upload through the browser dialog, as following image shows:
 
The basic way to upload files is using a component such as <javadoc>org.zkoss.zul.Fileupload</javadoc>, <javadoc>org.zkoss.zul.Button</javadoc>, <javadoc>org.zkoss.zul.Menuitem</javadoc>, <javadoc>org.zkoss.zul.Toolbarbutton</javadoc> and so on. <ref>for more detail, please refer to [[ZK Developer's Reference/UI Patterns/File Upload and Download]] and [[ZK Component Reference/Essential Components/Fileupload]]</ref> If we assign the <tt>upload</tt> attribute to these components, users can click and select a file to upload through the browser dialog, as following image shows:
Line 67: Line 67:
 
<references/>
 
<references/>
  
= HTML5 =
+
Since ZK 6.0.0
 +
= Upload Multiple Files with HTML5 supported browsers =
 +
 
 +
The components allowed uploading file also support uploading multiple files at once if these components have <tt>multiple=true</tt> falg and users use the web browsers supported HTML5. <ref>for more detail, please refer to [[ZK_Component_Reference/Essential_Components/Button#Upload]]</ref> Following is a typical example of multiple files uploading:
 +
 
 +
<source lang="java" start="10" high="14, 15, 16">
 +
@Test
 +
public void test(File[] files) throws Exception {
 +
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();
 +
}
 +
</source>
 +
* '''Line 14-16''': We can upload multiple files at once. Don't forget to invoke <tt>finish()</tt>method when the uploading is done. Notice that the instance of <tt>UploadAgent</tt> should be the same one.
 +
 
 +
 
 +
'''Notes'''
 +
<references/>
  
 
=Supported Components=
 
=Supported Components=

Revision as of 09:44, 29 June 2012

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

aowang



Since 1.1.0

ZATS Mimic introduces the UploadAgent to simulate file uploading operation with consistent usage. UploadAgent supports single or multiple files uploading as ZK components do.

Upload File with a Component

The basic way to upload files is using a component such as Fileupload, Button, Menuitem, Toolbarbutton and so on. [1] If we assign the upload attribute to these components, users can click and select a file to upload through the browser dialog, as following image shows: Zats upload button.png


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

@Test
public void test(File file) throws Exception {
	DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
	UploadAgent agent = desktop.query("#btn").as(UploadAgent.class);
	agent.upload(file, null);
	agent.finish();
	agent.upload(file, "text/plain");
	agent.finish();
	FileInputStream is = new FileInputStream(file);
	agent.upload(file.getName(), is, "text/plain");
	agent.finish();
	is.close();
}
  • Line 13: Cast component to UploadAgent and keep its reference.
  • Line 14: Invoke upload() method to upload a file. We can specify content type through the second argument and null value indicates binary form (application/octet-stream).
  • Line 15: Don't forget to invoke finish()method when the uploading is done. Notice that the instance of UploadAgent should be the same one.
  • Line 16: It specifies the content of file is a plain text. For more type definitions, please refer to Internet media type
  • Line 19, 21: The upload(String, InputStream, String) method won't close the input stream, we should close it manually.

Notes

Upload Files with the Static Method

Another way to upload files is that invokes the static method get() of Fileupload.[1] This static method will open a uploading dialog and allow users to upload single or multiple files (if configured), as following image shows.

Zats upload dialog.png

UploadAgent also support this kind of case. However, the static method was invoked according to event occurred. Therefore, we can cast DesktopAgent as a UploadAgent. Following is a typical example of file uploading with the static method:

@Test
public void test(File[] files) throws Exception {
	DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
	desktop.query("#label1").click();
	UploadAgent agent = desktop.as(UploadAgent.class);
	agent.upload(files[0], "text/plain");
	agent.finish();
	desktop.query("#label2").click();
	agent = desktop.as(UploadAgent.class);
	agent.upload(files[0], null);
	agent.upload(files[1], "image/png");
	agent.finish();
}
  • Line 13-15: After performed a event led to upload operation, we can cast DesktopAgent as a UploadFile for uploading. Don't forget to invoke finish()method when the uploading is done. Notice that the instance of UploadAgent should be the same one.
  • Line 19-21: If you invoke the static method of Fileupload and specify maximal allowed number, we can upload multiple files at once. Don't forget to invoke finish()method when the uploading is done. Notice that the instance of UploadAgent should be the same one.

Notes

Since ZK 6.0.0

Upload Multiple Files with HTML5 supported browsers

The components allowed uploading file also support uploading multiple files at once if these components have multiple=true falg and users use the web browsers supported HTML5. [1] Following is a typical example of multiple files uploading:

@Test
public void test(File[] files) throws Exception {
	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-16: We can upload multiple files at once. Don't forget to invoke finish()method when the uploading is done. Notice that the instance of UploadAgent should be the same one.


Notes

Supported Components

Components
Version
Note
DesktopAgent 5, 6


aowang




Last Update : 2012/06/29

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