Friday, August 8, 2008

JSP: Save As Pop Up in JSP

Step First:

Add the following method in the declaration tag of your JSP page.

<%!

void returnFile(String filename, OutputStream out)throws Exception
{
// open file here using File object
File inputFile =new File(filename);
// open stream from that file
FileReader in = new FileReader(inputFile);
int c;
while ((c = in.read()) != -1)
out.write(c);

// close the stream
in.close();
// close the file

}
%>

Step Two:

Add the following code segment in scriptlet tag.

response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=ASPTransBreakOut.txt");

// Send the file.
OutputStream outstr = response.getOutputStream( );
returnFile(fileLocation, outstr); // Method implemented in declarative tag in step first
outstr.flush();

WHERE:

ASPTransBreakOut.txt
is the name that will automatically show you up in SaveAs Popup menu

and

fileLocation

is the location of the actual fine on your server.

No comments: