File Upload and Download"

From Documentation
Line 44: Line 44:
  
 
=File Download=
 
=File Download=
 +
 +
[[ZK Component Reference/Essential Components/Filedownload|Filedownload]] provides a set of utilities to prompt a user for downloading a file from the server to a local folder.  For example,
 +
 +
<source lang="xml">
 +
<button label="Download calendar.pdf" onClick='Filedownload.save("/resources/calendar.pdf", null);'/>
 +
</source>
 +
 +
The file could be a static resource, an input stream, a file, a URL and others. Please refer to <javadoc>org.zkoss.zul.Filedownload</javadoc> for more information.
 +
 
=Version History=
 
=Version History=
 
{{LastUpdated}}
 
{{LastUpdated}}

Revision as of 10:38, 13 October 2011


File Upload and Download


File Upload

You could associate the upload feature to a component, such as Button, Toolbarbutton or Menuitem. In other words, you could make a button or a menu item to upload a file when the user clicks on it.

The implementation is straightaway:

  1. Assign the upload attribute to true for the component used to upload a file
  2. Assign an event listener to the component for the onUpload event[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 could control the maximal allowed number of files, the maximal allowed size and other information by use of Button.setUpload(String).

<menupopup>
    <menuitem label="Upload" upload="true,maxsize=-1,native"/>
</menupopup>

  1. 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.

File Download

Filedownload provides a set of utilities to prompt a user for downloading a file from the server to a local folder. For example,

<button label="Download calendar.pdf" onClick='Filedownload.save("/resources/calendar.pdf", null);'/>

The file could be a static resource, an input stream, a file, a URL and others. Please refer to Filedownload for more information.

Version History

Last Update : 2011/10/13


Version Date Content
     



Last Update : 2011/10/13

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