Win a copy of Modern JavaScript for the Impatient 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 all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

download a csv file from jsp

Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want user download some datas from database in form of *.csv.
I used <a href = "testdownload.jsp> download
to link testdownload.jsp that code as follow:

<%@ page import="*,javax.servlet.*,java.util.* " %>
String path ="../downfile/";
String filename="transaction.csv";
response.setHeader("Content-Disposition","attachment; filename=" + "transaction.csv");
int iRead;
FileInputStream stream = null;
try {File f = new File(path+filename);
stream = new FileInputStream(f);
while ((iRead = != -1) {
finally {
if (stream != null) {
It didn't work, I want user download trasactions.csv but the save as window
gave me information save testdownload.jsp
and then gave errors information can't save
does anybody know how to do it?
thanks in advance!
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in this thread....
Seems like the download works this way.
- satya
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi krussi,
just now i saw ur query,i dont know whether i have taken ur query in the right sense or not,but as far as i have understood i shall suggest u one thing,
u click the link which calls the xxx.jsp in which u have written a code to write to a transaction.csv.
hope i am right.
u do one thing,u provide a link in this jsp file which calls the same transaction.csv.
let me be more clear.
suppose u have created a file (csv) in
then give a link in the jsp file,
<a href="/<server-path>/transaction.csv">Open csv file </a>
so now when u click this link u will get a file download message box,i think this is what u needed
In short,
give a link in the jsp file,which calls the transaction.csv.
hope u r not confused.
if confused,reply soon.
Remember in the file download option u have a check box,stating always ask before opening such kind of file,let it be checked.
[ February 04, 2002: Message edited by: mahadevan raja ]
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried the following code, it works...

If you already have a file created then its better to directly give a link to it.
[ February 05, 2002: Message edited by: Les Dsouza ]
krussi rong
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everybody for help!
My task is let user download some datas from our
database in form of *.csv. when user click download link,it should be form a transactions.csv
file according to user's query automatically,then the browser give the user a file download window.
I tried many ways, but didn't work well.
First way:
I formed data file transactions.csv first, then
used <a href="transactions.csv> download
the problem is:
but when I click the link download, the browser sometimes give me a save as window, sometimes opened the transactions.csv directively.
Second way:
I used <a href="testdownload.jsp">download
link to testdownload.jsp that form data file
transactions.csv then give a user download window
let user download file. the testdownload.jsp code
I wrote in the top.
the problem is:
when I click the download link,the browser give me
a download testdownload.jsp window instead of trasaction.csv download window,then I click the button 'Ok',then give me error.
so what's the problem,how to do about that.maybe
I have something wrong about how to link a
pie. tiny ad:
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic