Hi all,

Inserting a simple <a href="brochure.pdf" download="true"> type link in a Tapestry template so visitors can download a PDF is straightforward enough.

I'd like to shift this to inside the controller, however, so that some code for analytical purposes can be added.

A quick Google search revealed the code example below, from around seven years ago. Just wondering whether this is still the right approach. Also, it's not clear how browsers might treat this 'stream response' - download or display?

Happy to see any better approaches.

Regards,

Chris.


public Object onBrochure(){
    final File getFile();
    final OutputStreamResponse response = new OutputStreamResponse() {

        public String getContentType() {
            return "application/pdf";
        }

        public void prepareResponse(Response response) {
response.setHeader ("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"");
        }

        @Override
        public void writeToStream(OutputStream out) throws IOException {
            try {
                InputStream in = new FileInputStream(file);
                IOUtils.copy(in,out);
                in.close();
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    };
    return response;
}

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to