Solved

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

Posted on 2009-07-02
6
330 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

806 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