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

Download file to a users PC

 
Steve Campbell
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've written a servlet which creates either a comma delimited file whose suffix is .csv, or a XML format file which ends with .xml. I create these using a FileWriter/BufferedWriter/PrintWriter combo and output to a directory under webapps\bax (using Tomcat). In my JSP program I have some HTML (<A Href) which downloads the file to the users pc (after a file save dialog box). I don't have a problem when the .csv file is clicked for download, but when the .xml is clicked, the file is output on the screen instead of the "file download" box. I tried changing the content type, but it didn't seem to help. Do I need to download these files by opening a port between the pc's and doing some kind of file io?
 
lee dalais
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thought of maybe zipping it? i guess using ZipOutputStream therefore when it gets downloaded it's smaller and the download box will appear.
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good question. I hope you get some more replies. All I know is that if the server doesnt know how to display the file it assumes you want to download. I did that with a .class file using < a href
I would like to learn more about this as well.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IE (and supposedly other browsers as well) is smart enough to check file extension as well as context type. Strangely, Excell is not smart enough to get a file from the browsercache unless it has an extension it understands, even when being passed the complete filename (it seems like Excell appends ".xls" to the filename if there is no known extension).
You might want to change both mime-type and extension and see what happens, but even then you may have problems with browsers that are smart enough to determine the mime-type on the fly based on the actual content of the file (IE especially might well try to parse the file itself first if no viewer is available and only give a 'save as' dialog if it fails (with XML it would never fail).
 
Frank Carver
Sheriff
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Basically, IE is too arrogant. You might also try the "content-disposition" header to see if you can fool IE into giving a save-as box for XML. Use the search facility here to look for examples ...
 
Joe Paolangeli
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have done this with .csv and .xml files.
Try adding:
response.setContentType("application/x-filler");
response.setHeader("Content-Disposition","attachment;filename="
+ someFileNameVar);
 
Steve Campbell
Ranch Hand
Posts: 41
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joe,
I had already done a search and tried your suggestion. When I add your suggestion into my JSP program, it doesn't seem to understand the file that I asked to download. I tried giving both an absolute and relative path to the download file. It seems that both times it tries to download whatever is in the address line of the web server. This is usually the servlet which initiated the JSP page.
Do you think I should be using some kind of "FTP" command?
 
Rayudu addagarla
Ranch Hand
Posts: 39
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,
any browser which has a plugin for that file extension ,it will open right in the browser. i guess there is no way to control other than asking the user to "rightclick and save target as..".
all latest versions of browsers have .xml plugin
hope this helps..
Rayudu
 
Joe Paolangeli
Ranch Hand
Posts: 73
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I actually have a file download process in a servlet, not a JSP. Let me know if you are interested in this solution. I am not sure about the plug-in stuff but I think the setHeader method will fake out the browser.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic