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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to use JSP to force file download..

Status
Not open for further replies.

infcc

Programmer
Aug 28, 2003
2
CA
Hey everyone..

kinda new to jsp, and I am having a bit of trouble getting this code to work. Basically, I am creating an image gallery, and the user needs to be able to download a file by clicking on the link, even if the file is a .jpeg or another file that usually opens in a browser. I am using jsp and xml/xslt.

The way I have been trying to do it, is to have a form on my xslt page that submits back to my index.jsp page. The problem, is that once I hit the download button (to submit the form) on my page, it tries to force the download of the index.jsp file, and not the file I am specifying. Take a look at the code below (i have tested all the variables and they are working:

<%
String hasSubmitted = request.getParameter(&quot;hasSubmitted&quot;);

if (hasSubmitted != null) {

String filepath = request.getParameter(&quot;getPath&quot;);
String filename = request.getParameter(&quot;getFile&quot;);

response.setContentType(
&quot;APPLICATION/X-DOWNLOAD&quot;);
response.setHeader(&quot;Content-Disposition&quot;,
&quot;attachment; filename=\&quot;&quot;
+ filename + &quot;\&quot;&quot;);

java.io.FileInputStream fileInputStream =
new java.io.FileInputStream(filepath
+ filename);
int i;
while ((i=fileInputStream.read()) != -1) {
out.write(i);
}
fileInputStream.close();
out.close();


}


%>


<xtags:style xml=&quot;<%=xmlDoc%>&quot; xsl=&quot;xsl/sigGallery.xsl&quot;/>

Anyways any help that you guys could give would be MUCH appreciated!

Thanks!
 
So you have a flow of control that uses an if() statement to download the file. Have you verified the expression evaluates to true?

Also you don’t want to send any other data after the end of the if statement so you might need to have the remaining code below it in the .jsp page in a corresponding else block.


-pete
 
Heya..

Yup the expression evaluates to true when the form is submitted.. basically what i desire to get is a save as download box with the file contained in the filepath and filename variables (those variables both have proper values).. now this code DOES come up with a save as box, but instead of having the values contained in the variables, it tries to download my index.jsp file..

I have tried putting this code on a page by itself as well and posting the form to that page, but the same thing happens (tries to download the jsp page)..

-chris
 
Put your value for the Content-Disposition header in a String variable and log it to System.out.println() before setting the header to validate what it is.

Running out of ideas [sadeyes]

I know this works so you have something out of line somewhere.

-pete
 
See if this code helps


<html>

<head>
<meta http-equiv=&quot;Content-Language&quot; content=&quot;en-us&quot;>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;application/???&quot;; charset=windows-1252&quot;>
<meta name=&quot;GENERATOR&quot; content=&quot;Microsoft FrontPage 4.0&quot;>
<title>New Page 1</title>

<script language=&quot;JavaScript1.2&quot;>
<!--

function saveImageAs(imgOrURL) {
if (typeof imgOrURL == 'object')
imgOrURL = imgOrURL.src;
window.win = open (imgOrURL);
setTimeout('win.document.execCommand(&quot;SaveAs&quot;)', 500);
}
//-->
</script>

</head>

<body>
<A HREF=&quot;javascript: void 0&quot; ONCLICK=&quot;saveImageAs(document.anImage); return false&quot; >save image</A> <IMG NAME=&quot;anImage&quot; SRC=&quot;file:///C:/java/tomcat/webapps/ROOT/veritas/logo.gif&quot; width=&quot;336&quot; height=&quot;84&quot;>


</body>

</html>

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top