001: /* Filedownload.java
002:
003: {{IS_NOTE
004: Purpose:
005:
006: Description:
007:
008: History:
009: Mon Apr 16 09:29:44 2007, Created by tomyeh
010: }}IS_NOTE
011:
012: Copyright (C) 2007 Potix Corporation. All Rights Reserved.
013:
014: {{IS_RIGHT
015: This program is distributed under GPL Version 2.0 in the hope that
016: it will be useful, but WITHOUT ANY WARRANTY.
017: }}IS_RIGHT
018: */
019: package org.zkoss.zul;
020:
021: import java.io.InputStream;
022: import java.io.Reader;
023:
024: import org.zkoss.util.media.Media;
025: import org.zkoss.util.media.AMedia;
026:
027: import org.zkoss.zk.ui.Desktop;
028: import org.zkoss.zk.ui.Executions;
029: import org.zkoss.zk.ui.sys.WebAppCtrl;
030: import org.zkoss.zk.au.out.AuDownload;
031:
032: /**
033: * File download utilities.
034: *
035: * @author tomyeh
036: * @see Fileupload
037: */
038: public class Filedownload {
039: /** Open a download dialog to save the specified content at the client.
040: */
041: public static void save(Media media) {
042: save(media, null);
043: }
044:
045: /** Open a download dialog to save the specified content at the client
046: * with the suggested file name.
047: *
048: * @param media the media to download
049: * @param flnm the suggested file name, e.g., myfile.pdf.
050: * If null, {@link Media#getName} is assumed.
051: */
052: public static void save(Media media, String flnm) {
053: final Desktop desktop = Executions.getCurrent().getDesktop();
054: if (flnm == null)
055: flnm = media.getName();
056:
057: final StringBuffer sb = new StringBuffer(32);
058: if (flnm != null && flnm.length() != 0) {
059: sb.append('/');
060: sb.append(flnm);
061: if (flnm.lastIndexOf('.') < 0) {
062: final String format = media.getFormat();
063: if (format != null)
064: sb.append('.').append(format);
065: }
066: }
067: final String uri = desktop.getDownloadMediaURI(media, sb
068: .toString());
069: ((WebAppCtrl) desktop.getWebApp()).getUiEngine().addResponse(
070: null, new AuDownload(uri));
071: }
072:
073: /** Open a download dialog to save the specified content at the client
074: * with the suggested file name.
075: *
076: * @param content the content
077: * @param contentType the content type (aka., mine type),
078: * e.g., application/pdf
079: * @param flnm the suggested file name, e.g., myfile.pdf.
080: * If null, no suggested name is provided.
081: */
082: public static void save(byte[] content, String contentType,
083: String flnm) {
084: save(new AMedia(flnm, null, contentType, content), flnm);
085: }
086:
087: /** Open a download dialog to save the specified content at the client
088: * with the suggested file name.
089: *
090: * @param content the content
091: * @param contentType the content type (aka., mine type),
092: * e.g., application/pdf
093: * @param flnm the suggested file name, e.g., myfile.pdf.
094: * If null, no suggested name is provided.
095: */
096: public static void save(String content, String contentType,
097: String flnm) {
098: save(new AMedia(flnm, null, contentType, content), flnm);
099: }
100:
101: /** Open a download dialog to save the specified content at the client
102: * with the suggested file name.
103: *
104: * @param content the content
105: * @param contentType the content type (aka., mine type),
106: * e.g., application/pdf
107: * @param flnm the suggested file name, e.g., myfile.pdf.
108: * If null, no suggested name is provided.
109: */
110: public static void save(InputStream content, String contentType,
111: String flnm) {
112: save(new AMedia(flnm, null, contentType, content), flnm);
113: }
114:
115: /** Open a download dialog to save the specified content at the client
116: * with the suggested file name.
117: *
118: * @param content the content
119: * @param contentType the content type (aka., mine type),
120: * e.g., application/pdf
121: * @param flnm the suggested file name, e.g., myfile.pdf.
122: * If null, no suggested name is provided.
123: */
124: public static void save(Reader content, String contentType,
125: String flnm) {
126: save(new AMedia(flnm, null, contentType, content), flnm);
127: }
128: }
|