How to Put a downloadable link on jsp
Hi All,
I need to put a link on a jsp.By cliking on that link user should be displayed with file download dialog to download an excel sheet.
I tried using <a href="path of excel">Download</a>.When i click on this link the content of the file is getting parsed into browser itself(in binaru junk).
I am using IE 6.0.
How can I achieve this.
Thanks
[389 byte] By [
SecfGuya] at [2007-11-27 10:30:34]

# 1
This is because the browser doesn't know what kind of content it's getting, it assumes that it's text/html and that's why, prints it all out.
Consider pointing the link to a servlet and have that servlet read and send the file to the user. Read this: http://forum.java.sun.com/thread.jspa?threadID=5194486&tstart=0
# 2
Thanks you very much for the response.
I think this is purely beacuse of IE6.0..becoz with earlier version of IE i used the same href and it used to prompt filedownload dialog with out any programming.
Thanks
# 3
It is possible...because I had the same problem and I checked in Firefox, IE 6 and IE 7. I got different behaviour even within the same browser during different runs
Since you change your users browsers, you have to change your code :)
It's not very difficult; so if you don't find a solution using simple links, you could look into this.
# 4
As the starting point to do this i am thinking to do like this..
<a href="./download.do">Download</a>
Now in my action i will write the logic specified by you.What i am expecting is ,,when the user clicks on that link the actual file will come from the server and show the file download dialog box.If this is correct then my requirement is met.
I have lot many different fields in the same jsp .I hope they will not be effected with this code.
# 5
That is pretty much how it'll work. When you use code similar to the one in the link I posted earlier, you'll get this behaviour, except in the case of missing files. I've not handled this case ( nor have I had the need to, since I create the files at the time of calling the servlet ).
What happens is that the servlet would write the contents of the file to the output stream of the response object and because you set the content type to something different from text/html, the browser recognizes it as an external file that the user should be shown a dialog of options for.
But you should take care to remember that it is browser specific behaviour in the case of the user clicking on 'Open'. The browser may launch an external application, such as in the case of .torrent files or may open it within the browser itself, if it has the appropriate plugins, as is often seen in the case of PDFs or even with plain text files.
A suggestion I can offer is to use the target = _blank attribute with the link so as to force the opening of a new window. In this case, if the file is sent properly, the browser would show a dialog. In all other cases, you can atleast be sure your current page would not be overwritten with the file contents ( in case of the user clicking Open or in case of an exception being thrown in your servlet ( if you don't handle it properly ))