javagwtfile-uploadrequestfactory

Upload files using GWT Request Factory


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


Solution

  • 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.