Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • 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 ...
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to download csv file

Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the code in my class that creates a .csv file on the server and now I'm trying to provide a link that will open up a save window where the user can download it to their own computer.

My link <a href="<%=URL_BASE%>/student_services/counsellors/statistics.csv">click here</a> where URL_BASE is a constant variable but it opens up an browser window with the contents of statistics.csv. If I right click and save target as it wants to save as a .html file and even though I can change the extension to .csv I'm not sure that it will save correctly.

Besides my supervisor wants a pop-up box that lets the user save to the destination they want when you click the link.

I've found some code on another site but its not clear enough to me on how to use it.

I got:
<%@ page language="java" import="*"%>
function openErr(file){
//alert("file "+file);

var windowParams="toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=20,height=20,left=0,top=0";
function toOpen(file) {
win1 ="", winName, windowParams);
window.setTimeout('toCloseWin()', 5000);

function toCloseWin(){


Then in jsp
// download
String filePath="";
String fileNameNew="statistics.csv";
// response.setContentType("application/x-msdownload");
// or
// response.setContentType("application/unknown");
response.addHeader("Content-Disposition", "attachment; filename="+fileNameNew);
File uFile= new File(filePath);
int fSize=(int)uFile.length();
FileInputStream fis = new FileInputStream(uFile);
PrintWriter pw = response.getWriter();
int c=-1;
// Loop to read and write bytes.

while ((c = != -1){
// Close output and input resources.
}catch(Exception e){

NOTE: I have no idea why there is code there that is commented out. I found response.setContentType("text/csv"); on my own and have replaced their options of
// response.setContentType("application/x-msdownload");
// or
// response.setContentType("application/unknown");
with it instead.

The link was
<a target="_blank" href="<%=URL_BASE%>/student_services/counsellors/statistics.csv">
and for some reason they added
<form name="downloadFrm" action="" method="post">
<INPUT TYPE="hidden" name="fileName">
under it.

Also included was a DownloadServlet class
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public final class DownloadServlet extends HttpServlet {
private static final String basePath = "/doc";

public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException,
String filePath = request.getPathInfo();
String filename = request.getParameter( "filename" );
if( filePath == null && filename != null ) filePath = "/" + filename;
if( filename == null ) filename = filePath;
if( filename != null ) filename = (new File(filename)).getName();

if( filePath != null ) {
InputStream in = null;
OutputStream out = null;
try {
in = getServletContext().getResourceAsStream(basePath + filePath);
if( in != null ) {
out = new BufferedOutputStream( response.getOutputStream() );
in = new BufferedInputStream( in );
String contentType = "application/unknow";
System.out.println( "contentType: " + contentType );
response.setHeader("Content-Disposition","attachment; filename=\"" + filename + "\"");
int c; while( ( ) != -1 ) out.write( c );
} finally {
if( in != null ) try { in.close(); } catch( Exception e ) {}
if( out != null ) try { out.close(); } catch( Exception e ) {}
response.sendError( HttpServletResponse.SC_NOT_FOUND );

The code is very confusing and I think filename should be statistics.csv so I have replaced it but not sure if I have in the right places.

Can anyone help?
[ October 07, 2005: Message edited by: Linda Thomas ]
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Linda,

I'm facing the exact same problem you posted. Please let me if you have found the solution for it. Thank you.
Posts: 67592
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
This would be more appropriate in the Servlets fourm. Moved.
A wop bop a lu bob a womp bam boom. Tutti frutti ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic