Link to home
Start Free TrialLog in
Avatar of MFCAnswer
MFCAnswer

asked on

How to download a text file i.e send the text file as attachment to client

I want to download a file on clicking a button. Here is my  button code

<input type="button" onClick="showdownloadwindow()" value="Download My file">


The java script showdownloadwindow() should invoke a jsp that has code to  construct the text file to be downloaded

Here is the javascript function

<script>
      
      function showdownloadwindow()
      {
            var url = 'http://<%=request.getServerName()%>'+':'+<%=request.getServerPort()%>+'/downloadtextfile.jsp';      
            window.open(url,"","");
      }
</script>

the downloadtextfile.jsp looks something like this



<%
            response.setContentType("text/plain");
            response.setHeader("Content-disposition","attachment; filename=myfile.txt" );
            BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream());
            String temp="Put some text string for now";
            bos.write(temp.getBytes());
            bos.flush();
            bos.close();
            response.flushBuffer();
 %>

i have two problems

1. when the button "Download My file" is clicked a separate window is opened and then the save file dialog box appears. How can I  change my code
  so that it only shows download window only ( not another browser window)

2. myfile.txt contains some error in addition to the temp string


Here is the error from myfile.txt


<BR><H3>Original Exception: </H3>
<B>Error Message: </B>SRVE0199E: OutputStream already obtained<BR>
<B>Error Code: </B>500<BR>
<B>Target Servlet: </B>/downloadtextfile.jsp<BR>
<B>Error Stack: </B><BR>
java.lang.IllegalStateException: SRVE0199E: OutputStream already obtained

<BR>&nbsp;&nbsp;&nbsp;&nbsp;
      at com.ibm.ws.webcontainer.srt.SRTServletResponse.getWriter&#40;SRTServletResponse.java:467&#41;

<BR>&nbsp;&nbsp;&nbsp;&nbsp;
      at org.apache.jasper.runtime.JspWriterImpl.initOut&#40;JspWriterImpl.java:170&#41;

<BR>&nbsp;&nbsp;&nbsp;&nbsp;
      at org.apache.jasper.runtime.JspWriterImpl.flushBuffer&#40;JspWriterImpl.java:163&#41;

<BR>&nbsp;&nbsp;&nbsp;&nbsp;
      at org.apache.jasper.runtime.PageContextImpl.release&#40;PageContextImpl.java:217&#41;

<BR>&nbsp;&nbsp;&nbsp;&nbsp;
      at org.apache.jasper.runtime.JspFactoryImpl.internalReleasePageContext&#40;JspFactoryImpl.java:149&#41;

<BR>&nbsp;&nbsp;&nbsp;&nbsp;
      at org.apache.jasper.runtime.JspFactoryImpl.releasePageContext&#40;JspFactoryImpl.java:117&#41;

<BR>&nbsp;&nbsp;&nbsp;&nbsp;
      at com.ibm._jsp._downloadtextfile._jspService&#40;_downloadtextfile.java:93&#41;

<BR>&nbsp;&nbsp;&nbsp;&nbsp;
      at com.ibm.ws.jsp.runtime.HttpJspBase.service&#40;HttpJspBase.java:88&#41;

<BR>&nbsp;&nbsp;&nbsp;&nbsp;

ASKER CERTIFIED SOLUTION
Avatar of boonleng
boonleng
Flag of Malaysia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Also, if not mistaken browser will directly open the file if the content-type is "text/plain".
try changing it to "application/octet-stream".