Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

saving MS Excel doco with correct name

 
prav ba
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all,
have an application which i wrote which generates a report and exports data to MS excel. When i attempt to open the doco without saving it first (by selecting 'Open' from the File Downloader), the application opens the doco with a weird name rather than the name i am setting in the application. Cant figure out why its doing that, has anyone come across this sort if problem before ??

Am using java 1.4.2 and MS Excel 2003 SP3

thanks in advance.
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You normally set the file name in the content disposition header. Most self-respected web browsers respects and takes its value.

Only some widely used web browser developed by a team in Redmond completely ignores it and takes the part after the last slash in the request URL as file name. If it isn't present, then a temporary file name will be generated. Sad, but true. Fortunately its market coverage continues shrinking as for now.

Best what you can do is to append the actual file name to the request URL. Something like http://example.com/webapp/fileservlet/filename.xls
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the reply Bauke. I did set the file name in the content disposition header ;

res.setHeader("Content-Disposition", contentDisposition + "; filename=" + contentFileName);

for some reason i cant get to the http://example.com/webapp/fileservlet/filename.xls URL.

thanks again.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe Bauke was showing an example of how to create a URL including the extension. It wasn't meant to be a real link.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure your code is quite right, as we don't know what the 'contentDisposition' variable is, and I'm not sure why it is a variable in any case:

res.setHeader("Content-Disposition", "attachment; filename=" + filename);
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It was indeed just an example URL.

If you want an actual code example, then you may find this useful: http://balusc.blogspot.com/2007/07/fileservlet.html

That said, donĀ“t forget to quote the filename in the content disposition header. Otherwise filenames with spaces become garbled as well.
 
nikil shar
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ah i see. thanks heaps for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic