-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Hi,
I want to offer a download of multiple files, which are compressed. Since I have limited space on the hdd, I want to zip the files while downloading.
This seems to be possible, if you write to the response's output stream (Creating Zip file while client is downloading) / (Compress dynamic content to ServletOutputStream).
I tried to write directly to the outputstream, but it fails with: (illegal character (SyntaxError))
HttpServletResponse response = ((HttpServletResponse) Executions.getCurrent().getNativeResponse());
response.setContentType("application/zip");
response.setHeader("Content-Disposition", "attachment;filename=test.zip");
try (ZipOutputStream out = new ZipOutputStream(response.getOutputStream())) {
byte[] readAllBytes = Files.readAllBytes(Paths.get("test.xlsx"));
ZipEntry zipEntry = new ZipEntry("test.xlsx");
zipEntry.setSize(readAllBytes.length);
zipEntry.setTime(System.currentTimeMillis());
out.putNextEntry(zipEntry);
out.write(readAllBytes);
out.closeEntry();
// You may add other files here if you want to
out.finish();
} catch (Exception e) {
e.printStackTrace();
}
Another, probably better option would be to use the Filedownload, but the only stream is an Inputstream.
Does anyone got an idea how to solve this? Thank you :)
Hi Ingo,
Executions.getCurrent.getResponse() will return the response object associated with the current /zkau request. You cannot use this to start a file download because by the time you get access to this object, ZK have already written into it, and will write again. Furthermore, the /zkau request is not made by the browser navigation, but as an Ajax request by the client engine, which is not equipped to process a binary file (it only expects JSON data as a reply).This is a limited communication channel between ZK server and client.
This mean that you cannot use the execution HttpResponse to download a file. To do this, the FileDownload utils in ZK open a separate request to a different AU Extension (sort of mini-servlet). To write to response directly, you would need to do something similar and send the client to load from your own servlet, or your own AuExtension.
If you'd like more details on that part, I'd recommend contacting support.
Asked: 2018-10-16 15:24:52 +0800
Seen: 11 times
Last updated: Oct 17 '18
How to stop combining commands in an ajax call ?
Is there a way to strip html comments from the response ?
How to control selectAll() feature in a Listbox with multiple=true and checkmark=true
ZK8 Wizard Series: Part 2 now released!
Faster And Lighter, The Performance Test On ZK8
org.zkoss.xel.XelException: Resource not found: http://www.zkoss.org/dsp/web/core