It's not a secret anymore!*
The moose likes Servlets and the fly likes Problem downloading CSV file in Internet Explorer. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problem downloading CSV file in Internet Explorer." Watch "Problem downloading CSV file in Internet Explorer." New topic
Author

Problem downloading CSV file in Internet Explorer.

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Hi All,

I am providing user an option to download CSV file from my application. If we don't save that file in the machine and instead of clicking save we click open in internet explorer it opens in Microsoft Excel and excel uses the file name as worksheet name. Now in this case what is happening that it shows me excel worksheet name as .csv[filename this is illegal name for excel worksheet and I get error for the same. In other case if I save this file on my machine and open it, it works fine. Can any one tell me is it the problem with my code or something else? I am using following content type to download this CSV file.



Pawan Chopra
SCJP - DuMmIeS mInD
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41114
    
  45
Google for "mime type csv" to find the correct MIME type of CSVs.


Ping & DNS - my free Android networking tools app
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

The Bellow example code will help you
Example


Java Best Practices
Linux Best Practices
Amortization Calculator

pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

Thanks Vinoth, but the example couldn't resolve my problem. I think it is the problem with Microsoft Excel because the same thing is working fine in Microsoft Excel 2007. I was trying in 2003 version.
Vinoth Thirunavukarasu
Ranch Hand

Joined: Dec 18, 2008
Posts: 164

I'm using Excel 2003. It works fine.
pawan chopra
Ranch Hand

Joined: Jan 23, 2008
Posts: 410

I think I have found the problem. actually what I have seen is when I am opening it in IE, it uses following naming convention to save file in temporary folder:

Portal_Activity_Visitors_1d_15-May-2009[1].csv

now [] this is not allowed for the name of excel worksheet. That is why I am getting error. But still I am not sure
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

IE works out what to do with a file based on the Content-Type and Content-Disposition headers. If you have correctly set both of those (as Ulf suggested earlier) it should be OK. Windows uses the registry HKEY_CLASSES_ROOT/MIME/Database/Content-Type to map the Content-Type header to an application. Windows also has a concept called MIME sniffing whereby it has an algorithm that attempts to workout the MIME type of a binary file by analysing the first n bytes with varying degrees of success. Assuming you have correctly set the Content-Type MIME sniffing should not be playing a part, but if you are on XP SP2 or above you can disable it to make sure.

The file name doesn't matter to Excel. I can open a file names foo[1].bar[2] in Excel 2003 no problem. The square brakets stuff you see is a file name pattern that Excel itself can generate. Are you sure the error is about the file name and not a security warning that files of a particular type cannot be opened from a particular location? There is a bug in IE6 that (for security reasons) prevents certain applications opening files in the default temp file space.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Problem downloading CSV file in Internet Explorer.
 
Similar Threads
Create .csv from .xls
jsp file name issue
Choosing 'open' in file download adds [1] to filename?
Problem with servlet streaming CSV files in WinXP SP2
File download Problem