[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2706
  • Last Modified:

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;

0
MFCAnswer
Asked:
MFCAnswer
  • 2
1 Solution
 
boonlengCommented:
You can window.location.href instead of window.open, this way there wont be any popup window. the download dialog will directly appear. Example:
<script>
     function showdownloadwindow()
     {
          var url = 'http://<%=request.getServerName()%>'+':'+<%=request.getServerPort()%>+'/downloadtextfile.jsp';    
          window.location.href=url;
     }
</script>


JSP is not design for file download, use Servlet instead.
Example:

public final class MyServlet extends HttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException
    {
          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(); ;
    }
}
0
 
boonlengCommented:
Also, if not mistaken browser will directly open the file if the content-type is "text/plain".
try changing it to "application/octet-stream".
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now