File Upload and Download"
m ((via JWB)) |
m (remove empty version history (via JWB)) |
||
(5 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
<javadoc>org.zkoss.zul.Button</javadoc>, <javadoc>org.zkoss.zul.Toolbarbutton</javadoc> and <javadoc>org.zkoss.zul.Menuitem</javadoc> support file upload out-of-box. In other words, you just need to enable it with the steps below: | <javadoc>org.zkoss.zul.Button</javadoc>, <javadoc>org.zkoss.zul.Toolbarbutton</javadoc> and <javadoc>org.zkoss.zul.Menuitem</javadoc> support file upload out-of-box. In other words, you just need to enable it with the steps below: | ||
− | # Specify < | + | # Specify <code>upload</code> attribute with <code>true</code> to enable file upload |
− | # Assign an < | + | # Assign an <code>onUpload</code> event listener to the component <ref>If you enabled the use of event threads, you could use <javadoc method="get()">org.zkoss.zul.Fileupload</javadoc> and related. For more information, please refer to [[ZK Developer's Reference/UI Patterns/Event Threads/File Upload|the Event Thread section]].</ref> |
For example: | For example: | ||
Line 59: | Line 59: | ||
− | <source lang='java' | + | <source lang='java' highlight='5'> |
public class UploadComposer extends SelectorComposer { | public class UploadComposer extends SelectorComposer { | ||
Line 77: | Line 77: | ||
=File Download= | =File Download= | ||
− | [[ZK Component Reference/Essential Components/Filedownload|Filedownload]] provides a set of utilities to prompt a user | + | [[ZK Component Reference/Essential Components/Filedownload|Filedownload]] provides a set of utilities to prompt a user to download a file from the server to a local folder. For example, |
<source lang="xml"> | <source lang="xml"> | ||
Line 87: | Line 87: | ||
The file could be a static resource, an input stream, a file, a URL and others. Please refer to [[ZK Component Reference/Essential Components/Filedownload|ZK Component Reference]] and <javadoc>org.zkoss.zul.Filedownload</javadoc> for more information. | The file could be a static resource, an input stream, a file, a URL and others. Please refer to [[ZK Component Reference/Essential Components/Filedownload|ZK Component Reference]] and <javadoc>org.zkoss.zul.Filedownload</javadoc> for more information. | ||
− | = | + | == File Download Link == |
− | + | Clicking a file download link terminates client engine to work, please refer to [[ZK_Component_Reference/Essential_Components/A#File_Download_Link]] for correct usages. | |
− | + | ||
− | + | ||
− | |||
− | |||
− | |||
− | |||
− | |||
{{ZKDevelopersReferencePageFooter}} | {{ZKDevelopersReferencePageFooter}} |
Latest revision as of 05:54, 6 February 2024
File Upload
Button, Toolbarbutton and Menuitem support file upload out-of-box. In other words, you just need to enable it with the steps below:
- Specify
upload
attribute withtrue
to enable file upload - Assign an
onUpload
event listener to the component [1]
For example:
<zk>
<zscript>
void upload(UploadEvent event) {
org.zkoss.util.media.Media media = event.getMedia();
if (media instanceof org.zkoss.image.Image) {
org.zkoss.zul.Image image = new org.zkoss.zul.Image();
image.setContent( (org.zkoss.image.Image) media);
image.setParent(pics);
} else {
Messagebox.show("Not an image: "+media, "Error", Messagebox.OK, Messagebox.ERROR);
}
}
</zscript>
<button label="Upload" upload="true" onUpload="upload(event)"/>
<vlayout id="pics" />
</zk>
You can control the maximal allowed number of files, the maximal allowed size, and other options. Please refer to org.zkoss.zul.Button.setUpload().
<menupopup>
<menuitem label="Upload" upload="true,maxsize=-1,native"/>
</menupopup>
- ↑ If you enabled the use of event threads, you could use Fileupload.get() and related. For more information, please refer to the Event Thread section.
Open File Upload Dialog
If you want the file upload on another component rather than those components above (Button, Toolbarbutton, Menu), you can call FileUpload.get() in an event listener.
Here is an example:
<a iconSclass="z-icon-upload" ...
apply="org.zkoss.reference.developer.uipattern.UploadComposer"/>
public class UploadComposer extends SelectorComposer {
@Listen(Events.ON_CLICK + "= a")
public void handleUpload(MouseEvent e) {
Fileupload.get(1, new EventListener<UploadEvent>() {
public void onEvent(UploadEvent event) throws Exception {
Media[] medias = event.getMedias();
System.out.println("upload " + medias[0].getName());
}
});
}
}
File Download
Filedownload provides a set of utilities to prompt a user to download a file from the server to a local folder. For example,
<button label="Download a file" onClick='Filedownload.save("~./zklogo.png", null);'/>
The file could be a static resource, an input stream, a file, a URL and others. Please refer to ZK Component Reference and Filedownload for more information.
File Download Link
Clicking a file download link terminates client engine to work, please refer to ZK_Component_Reference/Essential_Components/A#File_Download_Link for correct usages.