ZATS UploadAgent"

From Documentation
m (correct highlight (via JWB))
 
(4 intermediate revisions by one other user not shown)
Line 4: Line 4:
 
  Since 1.1.0
 
  Since 1.1.0
  
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. Following is typical steps:
+
ZATS Mimic introduces the <code>UploadAgent</code> to simulate file uploading operation with consistent usage. <code>UploadAgent</code> supports single or multiple files uploading as ZK components do. Following is typical steps:
# Obtain a <tt>UploadAgent</tt> instance according to your case. Notice that you should use the same instance in one uploading iteration.
+
# Obtain a <code>UploadAgent</code> instance according to your case. '''Notice that you should use the same instance in one uploading iteration.'''
# Upload a file. While uploading multiple files, repeat step 2.
+
# Upload a file by invoking <code>upload()</code> method.
# Invoke <tt>finish()</tt>method when the uploading is done.
+
# Invoke <code>finish()</code>method when the uploading is done.
  
= Upload File with a Component =
+
= Upload Files =
 +
 
 +
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 <code>upload</code> 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:
 
 
[[File:Zats_upload_button.png]]
 
[[File:Zats_upload_button.png]]
  
 +
'''Notes'''
 +
<references/>
 +
 +
== Upload Single File ==
  
We can cast these components as a <tt>UploadAgent</tt> and perform file uploading. Following is a typical example of single file uploading:
+
We can cast these components described above as <code>UploadAgent</code> and perform file uploading. Following is a typical example of single file uploading:
  
<source lang="java" start="10" high="13, 14, 15, 16, 19, 21">
+
<source lang="java" start="10" highlight="14, 15, 16">
 
@Test
 
@Test
public void test(File file) throws Exception {
+
public void test() throws Exception {
 +
File file = getFile();
 
DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
 
DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
 
UploadAgent agent = desktop.query("#btn").as(UploadAgent.class);
 
UploadAgent agent = desktop.query("#btn").as(UploadAgent.class);
agent.upload(file, null);
 
agent.finish();
 
 
agent.upload(file, "text/plain");
 
agent.upload(file, "text/plain");
 
agent.finish();
 
agent.finish();
FileInputStream is = new FileInputStream(file);
 
agent.upload(file.getName(), is, "text/plain");
 
agent.finish();
 
is.close();
 
 
}
 
}
 
</source>
 
</source>
* '''Line 13''': Cast component to <tt>UploadAgent</tt> and keep its reference.
+
* '''Line 14''': Cast component to <code>UploadAgent</code> and keep its reference.
* '''Line 14''': Invoke <tt>upload()</tt> 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''': Invoke <code>upload()</code> method to upload a file.
* '''Line 15''': Don't forget to invoke <tt>finish()</tt>method.
+
* '''Line 16''': Don't forget to invoke <code>finish()</code>method.
* '''Line 16''': It specifies the content of file is a plain text. For more type definitions, please refer to [http://en.wikipedia.org/wiki/Internet_media_type Internet media type]
 
* '''Line 19, 21''': The <tt>upload(String, InputStream, String)</tt> method  won't close the input stream, we should close it manually.
 
 
 
'''Notes'''
 
<references/>
 
 
 
= Upload Files with the Static Method =
 
 
 
Another way to upload files is that invokes the static method <tt>get()</tt> of <javadoc>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.
 
  
[[File:Zats upload dialog.png]]
+
== Upload Multiple Files ==
  
<tt>UploadAgent</tt> also support this kind of case. However, the static method was invoked according to event occurred. Therefore, we can cast <javadoc>org.zkoss.zk.ui.DesktopAgent</javadoc> as a <tt>UploadAgent</tt>. Following is a typical example of file uploading with the static method:
+
'''Since ZK 6.0.0''', the components allowed uploading file support uploading multiple files at once if they have <code>multiple=true</code> flag and users use the web browsers supported HTML5.<ref>for more detail, please refer to [[ZK_Component_Reference/Essential_Components/Button#Upload]]</ref> <code>UploadAgent</code> also supports multiple files uploading at once. Following is a typical example of multiple files uploading:
  
<source lang="java" start="10" high="13, 14, 15, 16, 19, 20">
+
<source lang="java" start="10" highlight="14, 15, 16, 17">
 
@Test
 
@Test
public void test(File[] files) throws Exception {
+
public void test() throws Exception {
 +
File[] files = getFiles();
 
DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
 
DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
desktop.query("#label1").click();
+
UploadAgent agent = desktop.query("#btn").as(UploadAgent.class);
UploadAgent agent = desktop.as(UploadAgent.class);
 
 
agent.upload(files[0], "text/plain");
 
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.upload(files[1], "image/png");
 
agent.finish();
 
agent.finish();
 
}
 
}
 
</source>
 
</source>
* '''Line 13-15''': After performed a event led to upload operation, we can cast <javadoc>org.zkoss.zk.ui.DesktopAgent</javadoc> as a <tt>UploadFile</tt> for uploading.
+
* '''Line 14''': Cast component to UploadAgent and keep its reference.
* '''Line 16''': Don't forget to invoke <tt>finish()</tt>method.
+
* '''Line 15-16''': We can upload multiple files at once when ZK version is greater than 6.0.0 and the component has <code>multiple=true</code> flag.
* '''Line 19-20''': If you invoke the static method of <javadoc>org.zkoss.zul.Fileupload</javadoc> and specify maximal allowed number, we can upload multiple files at once.
+
* '''Line 17''': Don't forget to invoke <code>finish()</code>method.
 +
 
  
 
'''Notes'''
 
'''Notes'''
 
<references/>
 
<references/>
  
= Upload Multiple Files with HTML5 supported browsers =
+
= Upload 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.
  
Since ZK 6.0.0
+
[[File:Zats upload dialog.png]]
  
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:
+
In this kind of case, we can retrieve <code>UploadAgent</code> 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">
+
<source lang="java" start="10" highlight="14, 15, 16, 17, 18">
 
@Test
 
@Test
public void test(File[] files) throws Exception {
+
public void test() throws Exception {
 +
File[] files = getFiles();
 
DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
 
DesktopAgent desktop = Zats.newClient().connect("/upload.zul");
UploadAgent agent = desktop.query("#btn").as(UploadAgent.class);
+
desktop.query("#label2").click();
 +
UploadAgent agent = desktop.as(UploadAgent.class);
 
agent.upload(files[0], "text/plain");
 
agent.upload(files[0], "text/plain");
 
agent.upload(files[1], "image/png");
 
agent.upload(files[1], "image/png");
Line 88: Line 79:
 
}
 
}
 
</source>
 
</source>
* '''Line 14-15''': We can upload multiple files at once.  
+
* '''Line 14-15''': After triggering an event led to upload operation, we can cast <javadoc>org.zkoss.zk.ui.DesktopAgent</javadoc> as a <code>UploadAgent</code> for uploading.
* '''Line 16''': Don't forget to invoke <tt>finish()</tt>method.
+
* '''Line 16-17''': We can upload multiple files at once when using <javadoc method="get()">org.zkoss.zul.Fileupload</javadoc>.
 
+
* '''Line 18''': Don't forget to invoke <code>finish()</code>method.
  
 
'''Notes'''
 
'''Notes'''

Latest revision as of 02:59, 20 January 2022

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. Following is typical steps:

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

Upload Files

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

Notes

Upload Single File

We can cast these components described above as UploadAgent and perform file uploading. Following is a typical example of 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.

Upload Multiple Files

Since ZK 6.0.0, the components allowed uploading file support uploading multiple files at once if they have multiple=true flag and users use the web browsers supported HTML5.[1] UploadAgent also supports multiple files uploading at once. Following is a typical example of multiple files uploading:

@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 when ZK version is greater than 6.0.0 and the component has multiple=true flag.
  • Line 17: Don't forget to invoke finish()method.


Notes

Upload Files with Fileupload.get()

Another way to upload files is to invoke the static method Fileupload.get().[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

In this kind of 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 led to upload operation, we can cast DesktopAgent as a UploadAgent for uploading.
  • Line 16-17: We can upload multiple files at once when 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


aowang



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