|
-
problem in downloading csv through servlet
Hi all!
I can download csv data to open in excel properly.
I'm using only IE 5.0 and above and jrun3.1 server.
I'm posting the data (generated on the client side) as follows:
<html>
<body>
<FORM name="csv_form" action="http://localhost:8000/csvpingserver/mycsvfile.csv"
method= "post">
<INPUT type= "hidden" value='a 70k chars looooong string'
name= "csv_data">
<INPUT id=btn_Download type=submit value=Download>
</FORM>
</body>
</html>
Servlet is as follows:
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
// Set Content Type
res.setContentType("application/ms-excel");
// Init Locals
String method = req.getMethod();
OutputStream os = res.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF8");
PrintWriter out = new PrintWriter(osw);
// Validate Method
if(method.compareToIgnoreCase("POST") != 0)
{
out.println("Error:");
out.close();
return;
}
try
{
// Get CSV Data
String strCsvData = req.getParameter("csv_data");
// Return CSV Data
if(strCsvData != null)
{
char [] chCsvData = strCsvData.toCharArray();
res.setHeader("Content-Disposition",
"attachment;filename=test.csv");
res.setIntHeader("Content-length", chCsvData.length);
out.write(chCsvData);
out.flush();
}
else
out.println("Error: No CSV Data found!");
out.close();
}
catch (Exception e)
{
System.err.println("Caught Exception: " + e.getMessage());
out.println("Error: Invalid Data sent to Servlet!");
out.close();
}
finally
{
if(out != null)
out.close();
if(osw != null)
osw.close();
}
}
The above code shows the open/save dialog TWICE (!?) before 'opening' it
in excel. This does not happen if I save it on the disk. I tried different
combinations of setContentType(), Content-Disposition, content-length, flush
so-on without success. Can any body give me the right combination of so that
a user can view it in excel (not in the browser)???
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks