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="showdownloadwindo w()" 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. getServerP ort()%>+'/ downloadte xtfile.jsp ';
window.open(url,"","");
}
</script>
the downloadtextfile.jsp looks something like this
<%
response.setContentType("t ext/plain" );
response.setHeader("Conten t-disposit ion","atta chment; filename=myfile.txt" );
BufferedOutputStream bos = new BufferedOutputStream(respo nse.getOut putStream( ));
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.IllegalStateExce ption: SRVE0199E: OutputStream already obtained
<BR> &nbs p;
at com.ibm.ws.webcontainer.sr t.SRTServl etResponse .getWriter (SRTSe rvletRespo nse.java:4 67)
<BR> &nbs p;
at org.apache.jasper.runtime. JspWriterI mpl.initOu t(JspW riterImpl. java:170&# 41;
<BR> &nbs p;
at org.apache.jasper.runtime. JspWriterI mpl.flushB uffer( JspWriterI mpl.java:1 63)
<BR> &nbs p;
at org.apache.jasper.runtime. PageContex tImpl.rele ase(Pa geContextI mpl.java:2 17)
<BR> &nbs p;
at org.apache.jasper.runtime. JspFactory Impl.inter nalRelease PageContex t(JspF actoryImpl .java:149& #41;
<BR> &nbs p;
at org.apache.jasper.runtime. JspFactory Impl.relea sePageCont ext(Js pFactoryIm pl.java:11 7)
<BR> &nbs p;
at com.ibm._jsp._downloadtext file._jspS ervice( ;_download textfile.j ava:93) ;
<BR> &nbs p;
at com.ibm.ws.jsp.runtime.Htt pJspBase.s ervice( ;HttpJspBa se.java:88 )
<BR> &nbs p;
<input type="button" onClick="showdownloadwindo
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(
window.open(url,"","");
}
</script>
the downloadtextfile.jsp looks something like this
<%
response.setContentType("t
response.setHeader("Conten
BufferedOutputStream bos = new BufferedOutputStream(respo
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<
<B>Error Stack: </B><BR>
java.lang.IllegalStateExce
<BR> &nbs
at com.ibm.ws.webcontainer.sr
<BR> &nbs
at org.apache.jasper.runtime.
<BR> &nbs
at org.apache.jasper.runtime.
<BR> &nbs
at org.apache.jasper.runtime.
<BR> &nbs
at org.apache.jasper.runtime.
<BR> &nbs
at org.apache.jasper.runtime.
<BR> &nbs
at com.ibm._jsp._downloadtext
<BR> &nbs
at com.ibm.ws.jsp.runtime.Htt
<BR> &nbs
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
try changing it to "application/octet-stream"