• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Static File Download Dialog Box : JSP

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The server sets these for you with static files.

What error are you seeing?
What App Server are you using?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"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
 
RS Gayathri
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you trying to run java code on the client, triggered by a javascript event?
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic