Download"
From Documentation
Line 16: | Line 16: | ||
'''DownloadComposer.java''' | '''DownloadComposer.java''' | ||
− | <source lang="java" start="10" high=" | + | <source lang="java" start="10" high="13"> |
public class DownloadComposer extends GenericForwardComposer { | public class DownloadComposer extends GenericForwardComposer { | ||
− | public void | + | @Listen("onClick=#btn") |
+ | public void download() throws IOException { | ||
Filedownload.save("/hello.txt", "application/octet-stream"); | Filedownload.save("/hello.txt", "application/octet-stream"); | ||
} | } |
Revision as of 01:29, 12 June 2012
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 {
@Listen("onClick=#btn")
public void download() 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
- ↑ for more detail, please refer to ZK_Component_Reference/Essential_Components/Filedownload
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:
- perform some operations through operation agents
- check is there a downloadable resource through desktop agent
- 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.