Robert's Tapestry 3's method works, but Tapestry 4's method might cause some problems, at least it did when I tried it. I believe it has to do with closing the response outputstream before rewind or the rest of rendering is complete. User will be able to download the file successfully, but you'll see an error in your logs about OutputStream is already closed (or something along those lines). To get around this error, I had to implement my own download engine service, as Shing suggested.

Dennis

Robert J. Walker wrote:
Here's a method that will do what you're asking:

// Tapestry 3
protected void download(IRequestCycle cycle, ByteArrayOutputStream content, 
String contentType, String filename) throws IOException {
    HttpServletResponse response = cycle.getRequestContext().getResponse();
    response.setHeader("Content-disposition", "attachment; filename=" + 
filename);
    response.setContentType(contentType);
    response.setContentLength(content.size());
    response.getOutputStream().write(content.toByteArray());
    response.flushBuffer();
    response.getOutputStream().close();
}

// Tapestry 4
protected void download(IRequestCycle cycle, ByteArrayOutputStream content, 
String contentType, String filename) throws IOException {
    WebResponse response = cycle.getInfrastructure().getResponse();
    response.setHeader("Content-disposition", "attachment; filename=" + 
filename);
    response.setIntHeader("Content-Length", content.size());
    OutputStream stream = response.getOutputStream(new 
ContentType(contentType));
    stream.write(content.toByteArray());
    stream.flush();
    stream.close();
}

You should make sure that the filename you provide is compatible with the 
user's operating system, or you could run into problems. It's also advisable to 
make the file extension jive with the content type so that the user doesn't 
have to rename the file to open it. Hope this helps.

Robert J. Walker


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to