Solved

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

Posted on 2009-07-02
6
328 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now