Slam
Slam

Reputation: 177

Upload files using GWT Request Factory

Is it possible to upload file via request factory? Simple example will be really helpful.

Upvotes: 2

Views: 811

Answers (3)

Actually you can!, I have an application doing it already.

  1. You need browsers supporting the FileApi (modern browsers do)
  2. You have to write some jsni code to read the file content into a base64 string.
  3. You will receive (asynchronously) a string which you can assign to any Bean attribute in your app and send it via RF, RPC, etc.

Here you have a copy/paste of the most significant code i use:

   public final native void readAsDataURL(MyClass that, FileUpload input) /*-{
     var files = [email protected]::getElement()().files;
     var reader = new FileReader();  
     reader.onload = function (evt) {
         [email protected]::done(Ljava/lang/String;)(evt.target.result);
     }
     reader.readAsDataURL(files[0]);
    }-*/;

It would be a comming-soon feature on my gwtupload library.

Upvotes: 7

Suresh Atta
Suresh Atta

Reputation: 121998

IMO RPC or Request Factory sense is XMLHttpRequest, which does not allow you encode and send local files to a server.

You need to write your own servlet and a GWT FormPanel .

complete example here with servlet and its mapping

Upvotes: -1

pillingworth
pillingworth

Reputation: 3238

No.

You need to create a separate file upload servlet. See Basic File upload in GWT.

Upvotes: 0

Related Questions