Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

"Save As" dialog box 1

Status
Not open for further replies.

tdion

MIS
Dec 18, 2003
61
US
How does one make the "Save As" dialog box appear with a JSP when they want to send the user a file? I have a tab delimited text file (that I have renamed "report.xls") that I would like them "Open With" the Excel program. Even though I renamed the file, it still sends the text to the browser and does not give the option to download.
 
Hi,

Set the content type of the page to
response.setContentType("application/vnd.ms-excel"); in the jsp page.

Cheers
Venu
 
Exactly where does that line of code go? How does one implement that command?

 
Friends,

After an entire day of research, I have located code that will prompt the user to download a file with a "Save As" dialog box. This can be inserted in a JSP because a JSP is really a servlet (don't let the imports fool you.)

Here it is...

<%@ page contentType="text/html"%>
<%@ page language="java"%>
<%@ page import="java.io.*"%>
<%@ page import="java.util.*"%>
<%@ page import="javax.servlet.*"%>
<%
if (request.getParameter("filename").equals("") == false) {
OutputStream o = response.getOutputStream();
String path =request.getParameter("path");
String filename=request.getParameter("filename");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;
filename="+request.getParameter("filename"));
int iRead;
FileInputStream stream = null;
try {
File f = new File(path+filename);
stream = new FileInputStream(f);
int ilength = 0;
while ((iRead = stream.read()) != -1)
{
o.write(iRead);
}
o.flush();
}
finally {
if (stream != null) {
stream.close();
}
}
}
%>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top