File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Static File Download Dialog Box : JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Static File Download Dialog Box : JSP" Watch "Static File Download Dialog Box : JSP" New topic
Author

Static File Download Dialog Box : JSP

RS Gayathri
Greenhorn

Joined: Oct 09, 2002
Posts: 2
Hi,

In my JSP page, upon a Button click, I should show a file download dialog box. This should download a static csv file.
Can someone help me with the code for the same ??

I have tried the same for a dynamically created csv file.
The code for that is as below.But this does not work for a static file.
response.setContentType("application/x-download");
response.setHeader("Content-Disposition","attachment;filename=" + Constants.CSV_FILE);

Can anyone help me pls..

Thanks
Gayathri
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The server sets these for you with static files.

What error are you seeing?
What App Server are you using?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

"gayathrirs",

We're pleased to have you here with us here on the Ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
bear
Forum Bartender


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
RS Gayathri
Greenhorn

Joined: Oct 09, 2002
Posts: 2
Thanks Ben,
I am using JRun4. Today I have changed the code and I am able to get the file download box.
But then I get an error in JRun console.

Details of the problem I am facing:
----------------------------------
My JSP page 'LoadExclusionCharges.jsp' has an 'Export' button.
Onclick of this button, a javascript function UploadAction() is invoked.
This JS fn. is expected to provide the file download dialog box.


Part of the code is given below:
// JS fn.
function UploadAction()
{
frmObj = ev al(document.forms[1]);
// java code
String uploadFlag = request.getParameter("upload");
if("true".equals(uploadFlag)){
File f = new File ("C:/Projects/ChargeMaster/cmdev/web/tempuploaddir/exportResult.csv");
response.setContentType ("application/text");
String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
response.setHeader ("Content-Disposition", "attachment;filename=\""+name+"\"");
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();
int bit = 256;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
}
//end of java code
frmObj.action="cm_default_action.do?destination=cm_load_exclusioncharges&upload=true";
frmObj.submit();
}

.
.
.
.
// html code calling the JS fn.
input type="button" name="Export" value="Export" class="graybutton" on click="UploadAction()"
.
.
.

With this code, when I click on the 'Export' button, the file gets opened.
But then I get an error in the console as : ANY IDEA WHY THIS ERROR IS THROWN
java.lang.IllegalStateException
at jrun.servlet.JRunResponse.getWriter(JRunResponse.java:195)
at jrun.jsp.runtime.JRunJspWriter.initPrintWriter(JRunJspWriter.java:76)
at jrun.jsp.runtime.JRunJspWriter.flush(JRunJspWriter.java:122)
at jrun.jsp.runtime.JRunJspWriter.flush(JRunJspWriter.java:111)
at java.io.PrintWriter.flush(PrintWriter.java:120)
at jrun.jsp.runtime.JRunJspWriter.flush(JRunJspWriter.java:131)
at jrun.jsp.runtime.JRunJspWriter.flush(JRunJspWriter.java:111)
at jrun.jsp.runtime.JRunJspWriter.check(JRunJspWriter.java:356)
at jrun.jsp.runtime.JRunJspWriter.write(JRunJspWriter.java:312)
at java.io.Writer.write(Writer.java:126)
at jrun.jsp.runtime.JRunJspWriter.print(JRunJspWriter.java:147)
at jrun.jsp.runtime.JspWriterWrapper.print(JspWriterWrapper.java:148)
at jrun__chargemaster__common__Header2ejsp1f._jspService(jrun__chargemaster__common__Header2ejsp1f.java:72)
at jrun.jsp.runtime.HttpJSPServlet.service(HttpJSPServlet.java:43)
at jrun.jsp.JSPServlet.service(JSPServlet.java:119)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invokeNext(JRunRequestDispatcher.java:439)
at jrun.servlet.JRunRequestDispatcher.include(JRunRequestDispatcher.java:371)
at jrun.jsp.runtime.JRunPageContext.include(JRunPageContext.java:355)
at jrun.jsp.tags.Include.doit(Include.java:35)
at jrun.jsp.tags.RequestDispatchTag.doEndTag(RequestDispatchTag.java:47)
at jrun__chargemaster__admin__LoadExclusionCharges2ejsp2c._jspService(jrun__chargemaster__admin__LoadExclusionCh
arges2ejsp2c.java:180)
at jrun.jsp.runtime.HttpJSPServlet.service(HttpJSPServlet.java:43)
at jrun.jsp.JSPServlet.service(JSPServlet.java:119)
at jrun.servlet.ServletInvoker.invoke(ServletInvoker.java:91)
at jrun.servlet.JRunInvokerChain.invokeNext(JRunInvokerChain.java:42)
at jrun.servlet.JRunRequestDispatcher.invokeNext(JRunRequestDispatcher.java:439)
at jrun.servlet.JRunRequestDispatcher.forwardInvoke(JRunRequestDispatcher.java:409)
at jrun.servlet.JRunRequestDispatcher.forward(JRunRequestDispatcher.java:178)
[ March 08, 2005: Message edited by: Gayathri RS ]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Are you trying to run java code on the client, triggered by a javascript event?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60997
    
  65

Gayathri RS, thanks for chaning your display name, but please re-read my message and the naming policy. You must have a first name and a last name; and while it is OK for your first name to be an initial, that is not OK for your last name. Please adjust your display name accordingly.

thanks!
bear
Forum bartender
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static File Download Dialog Box : JSP