Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Firefox appends .xls to a csv download  RSS feed

 
Las Avasa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to address a download CSV issue and one of the options I am trying is to write the results of a user request in comma-separated format to a static file and then redirect the user to a url pointing to the dynamically created file.

code snippet ...
-----------------------------------------------

ByteArrayOutputStream ostr = new ByteArrayOutputStream();

PrintWriter printW = new PrintWriter(ostr,true);

lformat.formatCSV((printW));

printW.flush();

String csvFileName = "SB_ABS"+ startTimeInMillis +""+actionUsrId+".csv";

String baseDir="/home/weblogic/bea/mydomain/undermydomain/excel/";
String csvFileFullName=baseDir + csvFileName;

PrintWriter csvWriter = new PrintWriter(new BufferedWriter(new FileWriter(csvFileFullName)));
csvWriter.print(ostr);
csvWriter.close();

response.sendRedirect("/excel/"+csvFileName);

------------------------------------------

I enclose data elements in quotes since they could contain commas. The download shows up fine in IE. However when Firefox is used, it is trying to open *.csv.xls and the data comes up in a single column in Excel displaying the quotes in them.

I have set the mime-type in web.xml as follows
---
<mime-mapping>
<extension>csv</extension>
<mime-type>application/vnd.ms-excel</mime-type>
</mime-mapping>

----


Can anyone suggest how I can get around this problem.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're only redirecting to your files to be served up as a static resource, then there isn't much you can do. The server and browser will do what they're going to do.

Is there any particular reason that you're writing to file and then redirecting to it instead of writing directly to the ServletOutputStream?

If you did write directly to the output stream, you could set the Content-Disposition tag which allows you to suggest the exact file name that the the browser should use.
 
Las Avasa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. Originally I was writing comma-separated content to the ServletOutputStream. But we have an issue when the request comes over a slow dial-up network. Part of the csv content is lost/ chopped off.

So, I was trying to see if serving the content as a static resource would help. But now I ran into the firefox issue.

The original code I had was ....
------------------------------------------------------------------------
ByteArrayOutputStream ostr = new ByteArrayOutputStream();

PrintWriter printW = new PrintWriter(ostr,true);

lformat.formatCSV((printW)); // iterates over serveral 1000 records and write comma-separated data to printW
printW.flush();

out.print(ostr);
out.flush();

lformat=null;
-----------------------------------------------------------------------

There are no browser formatting issues with the above code, except that for requests over a slow connection( < 14 kbps ), the downloaded file was incomplete.

I couldn't figure out how to fix this problem and have been trying alternatives/workarounds.
 
Paul Clapham
Sheriff
Posts: 22471
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your web.xml file, you have assigned to the .csv extension the MIME type that everybody else in the world assigns to the .xls extension. So, you are telling the browsers that what you are downloading is an Excel worksheet. But when the browser takes that advice seriously, you have a problem. So: don't tell the browser that the data is an Excel worksheet when it isn't one.
 
Las Avasa
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm. That would explain why Firefox appends a .xls to the file name and Excel is not translating the comma-separated values anymore. I looked for a mime-type for .csv that will allow us to associate Ms-Excel when the browser downloads it. I couldn't find one. If I remove the mime-type mapping, even IE fails now. The file is being opened inline in the browsers as-is (commas, quotes are being displayed in the browser window ).

I guess I could change the format to tab-delimited and name the file .xls and use the mime-type.

Would that work ? Is that a good practice ?

Thanks a lot for your comments/suggestions, they've been very helpful. I am still rying to see if my original code will work for slow n/w users. So if you have any thoughts on what i am doing wrong in my code, I'd really appreciate it.
[ July 23, 2007: Message edited by: Las Avasa ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!