Saturday, January 7

Export/download link

Want to keep the browser from displaying data you want the user to download? Try the code snapshot below on the controller:



String filename = ..............
byte[] content = ...............
String mimetype = this.getServletContext().getMimeType(filename);
response.setContentType(mimetype);
response.setContentLength(content.length);
response.setHeader("Content-Disposition","attachment; filename=\"" + filename +"\"");
FileCopyUtils.copy(content , response.getOutputStream());


The Content-Disposition header will force the browser to download the content as an attachment rather than display it on the browser.


No comments: