2

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

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
Slam
  • 177
  • 1
  • 8

3 Answers3

7

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 = input.@com.google.gwt.user.client.ui.FileUpload::getElement()().files;
     var reader = new FileReader();  
     reader.onload = function (evt) {
         that.@...MyClass::done(Ljava/lang/String;)(evt.target.result);
     }
     reader.readAsDataURL(files[0]);
    }-*/;

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

Manolo Carrasco Moñino
  • 9,723
  • 1
  • 22
  • 27
  • Alternatively you can also use the `Elemental` library. It should be possible to use most of the FileAPI (i.e FileReader) without JSNI. – Ümit Apr 11 '13 at 09:21
  • I tried Elemental and I found some problems, one was that it is very specific of chrome, and second I had a problem with creating some objects in FF (i dont remember now), so did I prefer a generic implementation for all browsers supporting the fileapi – Manolo Carrasco Moñino Apr 11 '13 at 11:15
  • 1
    You are right. There are some chrome specifics in there. But I really like the API because it is so close to the javascript API/spec. So you can easily take an example from HTML5 rocks and port it to `Elemental` quickly. Hopefully with time most vendor prefixes get removed. Then Elemental could easily support different browsers – Ümit Apr 11 '13 at 11:39
  • Yeah, that is something I miss: a common module for most modern browsers, and a specific module for chrome. Some of my experiments work with webkit, FF and IE9, but most of them failed in some of them because trivial syntax issues. Another problem is that it didn't work for me in [hosted-mode](http://code.google.com/p/google-web-toolkit/issues/detail?id=7842). And finally the lack of info about problems. – Manolo Carrasco Moñino Apr 11 '13 at 15:48
0

No.

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

Community
  • 1
  • 1
pillingworth
  • 3,238
  • 2
  • 24
  • 49
-1

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

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307