aspose file tools*
The moose likes JSP and the fly likes How can I download a file from a jsp? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How can I download a file from a jsp?" Watch "How can I download a file from a jsp?" New topic
Author

How can I download a file from a jsp?

Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hi All,
I will have a jsp page that knows the path and name to a PDF file on the server machine. I want to provide a "download" button so that a user can get the file onto thier client machine. They won't want to open/look at the file, just save it to thier client machine. The part that I am confused by right now is, how can I let them navigate the windows folder system so they can specify what folder to save the file to and what to name the file?
Thank you for any pointers,
Kim
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The Content-Disposition header allows you to mark a response as either "inline" or "attachment".

The "attachment" value suggests that the browser open the "What do you want to do with this?" dialog. If the machine has the proper application installed (Adobe Acrobat for instance), the user can choose to either open it or save it.

There is an example of this tag in the JSP FAQ.
Look for JspAndExcel.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Thank you so much for your speedy reply Ben.
It sort-of worked, I am sure I must have gotten something a bit wrong.

I put this at the top of my jsp page :


and then, when I click save, a save did happen, but it appears that what was saved was the source of the jsp page, not the C:\blah.sql file that I intended to save.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

How're you sending the file to the browser?
Kim Kantola
Ranch Hand

Joined: May 17, 2001
Posts: 274
Hmm, I guess I am not sending the file to the browser, doh!

I was hoping I could just prompt the user to tell me what they want to name the file, and where they wanted to put it, and then on the java side I could save the file to that location, not have to send it to the jsp. One the java side I will have a handle on the file location and name on the server machine.

Not sure if this is doable though. Sorry, I am new to this stuff, very much appreciating your help.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61426
    
  67

You cannot prompt the user for a file location. What will happen is that, if the content disposition is correctly set, the browser itself will open a File Save dialog when the file is sent to the browser. The user can then pick where the file is to be saved. You do not get to participate in this process.
[ June 29, 2007: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can I download a file from a jsp?