Download"

From Documentation
(Created page with "{{Template:UnderConstruction}} {{ZATSEssentialsPageHeader}} Since 1.1.0 We usually perform the file downloading through the <javadoc>org.zkoss.zul.Filedownload</javadoc> when ...")
 
Line 6: Line 6:
 
We usually perform the file downloading through the <javadoc>org.zkoss.zul.Filedownload</javadoc> when some events occurred<ref>for more detail, please refer to [[ZK_Component_Reference/Essential_Components/Filedownload]]</ref>. The following is a typical example about downloading a file:
 
We usually perform the file downloading through the <javadoc>org.zkoss.zul.Filedownload</javadoc> when some events occurred<ref>for more detail, please refer to [[ZK_Component_Reference/Essential_Components/Filedownload]]</ref>. The following is a typical example about downloading a file:
  
<source lang="java" high="4">
+
'''download.zul'''
 +
<source lang="xml">
 
<zk>
 
<zk>
<button id="btn" label="download">
+
<div apply="DownloadComposer">
<attribute name="onClick"><![CDATA[
+
<button id="btn" label="download" />
org.zkoss.zul.Filedownload.save("foo.txt", "application/octet-stream");
+
</div>
]]>
 
</attribute>
 
</button>
 
 
</zk>
 
</zk>
 +
</source>
 +
 +
'''DownloadComposer.java'''
 +
<source lang="java" start="10" high="12">
 +
public class DownloadComposer extends GenericForwardComposer {
 +
public void onClick$btn() throws IOException {
 +
Filedownload.save("/hello.txt", "application/octet-stream");
 +
}
 +
}
 
</source>
 
</source>
  
Line 29: Line 36:
 
# fetch and verify the information or content of the resource
 
# fetch and verify the information or content of the resource
  
<source lang="java" high="8,9,10,11,12,13">
+
<source lang="java" start="10" high="14,15,16,17,18,19">
 
@Test
 
@Test
 
public void test() throws Exception {
 
public void test() throws Exception {
Zats.init(".");
+
DesktopAgent desktop = Zats.newClient().connect("/download.zul");
try {
+
Assert.assertNull(desktop.getDownloadable());
DesktopAgent desktop = Zats.newClient().connect("/foo.zul");
+
desktop.query("#btn").click();
Assert.assertNull(desktop.getDownloadable());
+
Resource resource = desktop.getDownloadable();
 
+
Assert.assertNotNull(resource);
desktop.query("#btn").click();
+
Assert.assertEquals("hello.txt", resource.getName());
Resource resource = desktop.getDownloadable();
+
String content = fetchContent(resource.getInputStream());
Assert.assertNotNull(resource);
+
Assert.assertEquals("Hello world!", content);
Assert.assertEquals("hello.txt", resource.getName());
 
String content = fetchContent(resource.getInputStream());
 
Assert.assertEquals("Hello world!", content);
 
} finally {
 
Zats.end();
 
}
 
 
}
 
}
 
</source>
 
</source>
 
+
* '''Line 14-16''': Because ZATS Mimic handles the response from ZK application automatically, we can get the current downloadable resource file from the <tt>DesktopAgent.getDownloadable()</tt>. The method might return <tt>null</tt> when getting the downloadable resource, it indicates that there is no downloadable resource after the previous operation.
* '''Line 6, 9, 10''': Because ZATS Mimic handles the response from ZK application automatically, we can get the current downloadable resource file from the <tt>DesktopAgent.getDownloadable()</tt>. The method might return <tt>null</tt> when getting the downloadable resource, it indicates that there is no downloadable resource after the previous operation.
+
* '''Line 17-19''': We can get more information from <tt>Resource</tt>, or fetch the content of resource file in binary through the input stream.
 
 
* '''Line 11-13''': We can get more information from <tt>Resource</tt>, or fetch the content of resource file in binary through the input stream.
 
  
 
{{ZATSEssentialsPageFooter}}
 
{{ZATSEssentialsPageFooter}}

Revision as of 09:23, 11 June 2012

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


Since 1.1.0


We usually perform the file downloading through the Filedownload when some events occurred[1]. The following is a typical example about downloading a file:

download.zul

<zk>
	<div apply="DownloadComposer">
		<button id="btn" label="download" />
	</div>
</zk>

DownloadComposer.java

public class DownloadComposer extends GenericForwardComposer {
	public void onClick$btn() throws IOException {
		Filedownload.save("/hello.txt", "application/octet-stream");
	}
}

Actually, the downloaded mechanism is a process with two steps. When you invoke save(), the Filedownload simply notifies ZK client engine of the downloaded URL. Then, the ZK client engine downloads such files according to the received URL.

Notes

Download files in a ZATS Mimic test case

In order to simulate same behavior as ZK client engine doing, ZATS Mimic introduces the Resource interface. It represents a downloadable resource file at server. The typical steps for testing downloading are:

  1. perform some operations through operation agents
  2. check is there a downloadable resource through desktop agent
  3. fetch and verify the information or content of the resource
@Test
public void test() throws Exception {
	DesktopAgent desktop = Zats.newClient().connect("/download.zul");
	Assert.assertNull(desktop.getDownloadable());
	desktop.query("#btn").click();
	Resource resource = desktop.getDownloadable();
	Assert.assertNotNull(resource);
	Assert.assertEquals("hello.txt", resource.getName());
	String content = fetchContent(resource.getInputStream());
	Assert.assertEquals("Hello world!", content);
}
  • Line 14-16: Because ZATS Mimic handles the response from ZK application automatically, we can get the current downloadable resource file from the DesktopAgent.getDownloadable(). The method might return null when getting the downloadable resource, it indicates that there is no downloadable resource after the previous operation.
  • Line 17-19: We can get more information from Resource, or fetch the content of resource file in binary through the input stream.



Last Update : 2012/06/11

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