I have found some code within this forum that I have been attempting to use to allow customers to download text files to their PC's. The code below is what I have come up with from my understandings on exactly how it should work, but it just will not work ...
Am I correct in assuming that the file that I want to make available for download is specified within the
File f = new File(path+filename); section???
I have made the
path variable refer directly to the file system (/disk2/invoice/) as well as via http (http://domain.com/invoice/) but it will not work !!!
It returns the save/open dialog but as soon as I select an option it returns a windows error prompt as follows:
.
Internet Explorer cannot download ...p?filename=123414_76453_437 from www.domain.com.
.
Internet Explorer was not able to open this Internet site. The requested site is either unavailable or cannot be found. Please try again later.
.
Can someone please tell me, where am I supposed to reference the file to be downloaded and how am I to reference it ???
<%
// get the file name from the calling page
String filename = request.getParameter("filename");
.
//get the file path to the file I want to make available via download
String path = getServletContext().getInitParameter("invoicePath");
.
response.setContentType("text/plain");
response.setHeader("Content-Disposition","attachment; filename=\""+filename+"\";");
.
int iRead;
FileInputStream stream = null;
try {
File f = new File(path+filename);
stream = new FileInputStream(f);
while ((iRead = stream.read()) != -1) {
out.write(iRead);
}
out.flush();
}
finally {
if (stream != null) {
stream.close();
}
}
%>
<%@ page import="java.io.*,javax.servlet.*,java.util.* " contentType="text/html" %>
<html>
finally we have success ...
</html>