views:

330

answers:

1

Hello Guys

I am creating a simple project that will allow me to upload and download files using gwt. i am having trouble with the downloading of files that are on my server.

For the file upload i used http://code.google.com/p/gwtupload/ and followed the instructions there. My file is stored on the server outside of the website container(on the hard drive),

Now when it comes to the downloading of a file, i want a user to press a download button and whatever item is currently selected will download. i dont really know how this will be done

i know i need a download servlet

public class DownloadAttachmentServlet extends HttpServlet {

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    super.doGet(req, resp);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

    String fileName = (String) req.getSession().getAttribute("fileName");

    YFUser user = (YFUser) req.getSession().getAttribute(TestServiceImpl.SESSION_USER);
    if (user == null)
        throw new ServletException("Invalid Session");

    InputStream in = null;
    OutputStream out = resp.getOutputStream();

    FileInputStream fIn = new FileInputStream(fileName);
    byte[] buffer = new byte[4096];
    int length;
    while ((length = in.read(buffer)) > 0){
        out.write(buffer, 0, length);
    }
    in.close();
    out.flush();

}
}

for the moment i will just pass a fileName string to retrieve the file for testing

now i am lost at what to do on the client side, i have a simple

public class DownloadFilePanel extends Composite {


public DownloadFilePanel(final YFUser user , final String fileName){

    final Element downloadIframe = RootPanel.get("__download").getElement();

    VerticalPanel content = new VerticalPanel();
    content.add(new Label("Download For this File : " + fileName));

    Button button = new Button("Download");
    button.addClickHandler(new ClickHandler(){
        @Override
        public void onClick(ClickEvent event) {
            // i do not know what to do here
    });
            content.add(button);
    initWidget(content);


}


}

above is a simple widget that will supply a panel that will allow for the download of a file based on a fileName

as you can see above, i do not know what to do to be able to download the file

is there any one that can point me in the right direction?

+1  A: 

On the client side, just create a regular <a href="path/to/servlet"> tag. You can use the Anchor class if you want to dynamically create it. When the user clicks the link, the browser will automatically download the file.

sri
OK so i can use an anchor, but how would i link this up to the download servlet and how would i pass the name of file as a string to the servlet aswell?
molleman
sri
thank you sri , works well now
molleman