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

Using Servlet to send down a file

 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have a Servlet (called Download) which send down a xml file (based on some query string).
My question is how can I specify the name of the file on users's desktop when people hit my servlet?

Right now, it saves into a file called 'Download'.

Thank you.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You set a Content-Disposition header like this:

response.setHeader("Content-Disposition", "attachment; filename=\"file.xml\"");
 
Jeroen T Wenting
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but browsers can ignore it, and so can users when they save it.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are sending a file which the browser can open , then browser wouldnot give you the option of saving the file ; rather this would try to open the file.(Like html , xml , and pdf [if acrobat reader is installed.]).
In case you want to send an xml , it would be read by the browser rather then giving the use an option to save this file.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
then browser wouldnot give you the option of saving the file

Only if the user wants it so. The first time the browser sees an unknown file format it will ask the user what to do with it. If the user specifies that files of that type should be saved to disk, then the browser will not try to open them.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are sending a file which the browser can open , then browser wouldnot give you the option of saving the file


When the browser can open a file (in other words the browser understands the format of the data.) then it opens the files directly ,rather giving the user the option to save it.
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When the browser can open a file (in other words the browser understands the format of the data.) then it opens the files directly


This is incorrect. Please re-read my previous post. Files downloaded as described above will not be opened -no matter which file type they are- unless they user specifically requested that at some point. Until she does, the file save dialog will be shown.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ulf , I got it now.
But we have to set the content-disposition header for this to happen.Is that so ?
 
Ulf Dittmer
Rancher
Pie
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic