File upload

296 views
Skip to first unread message

Sreekanth Nambiar

unread,
Nov 27, 2010, 5:36:53 AM11/27/10
to google-we...@googlegroups.com
Hi,

What is the best approach to upload a file?

1. Is it mandatory to submit the form? How should i implement the Remote Service in this case?

2. Can i upload the file with an asynchronous call? What should be the input parameters to the Remote service  method in this case?


Can anybody help me with some sample code?

Thanks
Sreekanth

Jim Douglas

unread,
Nov 27, 2010, 3:03:50 PM11/27/10
to Google Web Toolkit
Yes, you must submit the form to upload a file (or files), and the
file(s) must be selected by direct action of the user. The basic
structure is:

On the client side, a GWT FormPanel containing a FileUpload widget
(and optionally other information, e.g. one or more Hidden fields
containing information that will be meaningful to your servlet).

http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/ui/FileUpload.html
http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/ui/FormPanel.html
http://google-web-toolkit.googlecode.com/svn/javadoc/2.1/com/google/gwt/user/client/ui/Hidden.html

Let's assume that your servlet URL is /fileUploadServlet. Your
FormPanel needs to do something like this:

private static final String SERVLET = "/fileUploadServlet";

setAction(SERVLET);
setEncoding(ENCODING_MULTIPART);
setMethod(METHOD_POST);

One the server side, you want to install the Apache FileUpload package
from here:

http://commons.apache.org/fileupload/

Using those helper functions in that package, the doPost method in
your FileUploadServlet class becomes straightforward. Just choose
from one of these examples:

http://commons.apache.org/fileupload/using.html
http://commons.apache.org/fileupload/streaming.html
Reply all
Reply to author
Forward
0 new messages