Download file to a users PC

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?

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.

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).

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

Joined: Dec 18, 2000
Posts: 41

posted Mar 06, 2001 10:41:00

0

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?

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

Good luck for Certfication.

Joe Paolangeli
Ranch Hand

Joined: Apr 05, 2000
Posts: 73

posted Mar 06, 2001 18:41:00

0

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.