Solved

Client side Download? Need user to be able to download files!

Posted on 2009-07-02
6
331 Views
Last Modified: 2012-05-07
Hi Experts.

I am building a web application using Java, JSF, IceFaces, and Apache Tomcat.

In this application, at some point I need the user to be able to download a file uploaded on to the server. I have tried a few examples online but none of them have been of success.

Essentially, the user should be able to open/download the file when they click its link (usually a commandLink tag or anything similar). I have some knowledge about this topic. The examples I saw online led to the browser handling the downloading (like FireFox and IE prompting a user to either "save" or "open" the file in a pop-up box). That or anything similar would be great.

Please help me complete this task by a useful sample code, instructions, and/or some website links.

Thanks in advance!
0
Comment
Question by:kingsha
  • 4
6 Comments
 
LVL 13

Expert Comment

by:Murali Murugesan
ID: 24769238
<a href="/someaction.do?attchmntId=1&atthFileName='EE_JSP.txt'">EE_JSP.txt</a>

>>someaction.do

 byte docBytes[] =readBlob(request.getParameter("attchmntId"));
            if (docBytes != null)
            {
                response.setHeader("Content-Disposition","attachment;filename=\""+ request.getParameter("atthFileName")+ "\";");
                response.setContentType("application/save-as-dialog");
                OutputStream outStream;
                try {
                    outStream = response.getOutputStream();
                    outStream.write(docBytes);
                    outStream.close();
                } catch (IOException e) {
               //log error
                }
            }

Hope this helps.

-Murali*
0
 
LVL 7

Expert Comment

by:nikhilmenon
ID: 24787918
Well perhaps a slightly easier way to do this is to directly provide a link to the file on your webpage. If you have everything saved on the server at "MyWebApp/xyz.jpg" you could merely add code similar to:

<a href="http://mysite.com/MyWebApp/xyz.jpg">Download the file!</a>

Cheers.
0
 
LVL 2

Author Comment

by:kingsha
ID: 24790915
Hi guys. Thanks for the replies. Have been busy with other stuff but wIll try these out ASAP. Will get back to you.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 2

Author Comment

by:kingsha
ID: 24796339
@ nikhilmenon

I tried your suggestion but cannot get it to work. I have tried placing a sample jpeg file called "pic1" into just about every folder in the apache servers directory. But still nothing doing.

For example:
<a href="http://localhost:8080/wtpwebapps/bronte/stuff/pic4.jpg">Download the file!</a>

WIll try to implement Murali's suggestion now.
0
 
LVL 2

Author Comment

by:kingsha
ID: 24796658
@ murali

What class are you using for the "request" and "response" functions you mentioned in your code. Java suggests certain libraries but all of them give me errors.

P.S...i raised points for this question to the max i have. I hope that attracts more help :(
0
 
LVL 2

Accepted Solution

by:
kingsha earned 0 total points
ID: 24798094
OK finally I got it! :D

Unfortunately none of the solutions provided worked for me. Muralis solution called for implementing a servlet to handle the downloading. I am completely unaware of how a servlet works and could not get his method to work.

The easier and most efficient solution was to use an iceFaces attribute. Its called an outputresource attribute. Here is a link for more information:
http://pillcase.blogspot.com/2008/11/how-to-use-iceoutputresource-in.html

Cheers everyone. Appreciate the input though.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
eclipse console opening separately 2 29
CSS: How do I override in-line styling 11 26
Why "Mobile First"? 5 20
What browser will run Java? 7 73
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
The viewer will learn how to implement Singleton Design Pattern in Java.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question