DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Returning A File As An Attachment From JSP

05.24.2006
| 10298 views |
  • submit to reddit
        // This is truly a snippet. It's a small piece of code you use
// within a JSP file to make sure that the file you are
// returning is not interpreted by the browser. You use this
// when you want to return a TXT file, JPG or other file that
// the browser might open on its own rather than offer a 
// dialog to the user so it can be saved.
//
// Note: In this example I'm returning a text file so I set 
// the MIME type of the response to text/plain, but you need 
// to make sure it matches the type of data you are returning.

response.setContentType("text/plain");

response.setHeader("Content-Disposition", "attachment; filename=\"test.txt\"");
    

Comments

Hudhaifa replied on Wed, 2006/11/08 - 4:36pm

Coooooooooooooooooooooooooolllllllllllllllllllllll