Wednesday, November 27, 2013

Re: GWT FileUpload with Progress Listener

See https://code.google.com/p/google-web-toolkit/issues/detail?id=624 comment 23. I've implemented a file upload with a listener and had this problem and solved it using the workaround described there.

Juan


2013/11/27 confile <michael.gorski82@googlemail.com>
Hi Jens, 

thanks for your help. I know the concept but did not fully understand you. 

1. What do you mean by: "Don't forget to cleanup the event listener after you are done."?

2. Did I get you right on the following. Since I use the progress for file upload I have to use a FormPanel. 

my progress listener class would be like: 

class MyProgressListener {
      void onProgress(int loaded, int total) {
         // so something with the data
      }
}

then I would do something like: 

final FormPanel form = new FormPanel();
form.setAction("/myFormHandler");
form.setEncoding(FormPanel.ENCODING_MULTIPART); 
form.setMethod(FormPanel.METHOD_POST);

    VerticalPanel panel = new VerticalPanel();
    form.setWidget(panel);

FileUpload upload = new FileUpload();
upload.setName("file");
panel.add(upload);

// Add a 'submit' button.
    panel.add(new Button("Submit", new ClickHandler() {
      public void onClick(ClickEvent event) {
        form.submit();
      }
    }));


3. How do I integrate the setProgressListener method in the FormPanel?

Thanks for help.


Am Mittwoch, 27. November 2013 16:51:45 UTC+1 schrieb Jens:
Well if you already know the concept of JSNI then you can use it to add a listener to GWT's XMLHttpRequest as its an ordinary JavaScriptObject. You either extend it or create a utility method. Something along these lines (probably not fully correct):

public native void setProgressListener(MyProgressListener p) /*-{
  this.upload.addEventListener("progress", function(event) {
    p.@com.example.progress.MyProgressListener::onProgress(II)(event.loaded, event.total);
  }, false);
}-*/;

A utility method would have an additional XMLHttpRequest parameter and use request.upload instead of this.upload.

Don't forget to cleanup the event listener after you are done. Also note that XMLHttpRequest.create() can return an IE specific object which might not support the progress feature or which requires you do register the listener differently. So your implementation might be a bit more complicated depending on your browser support requirements.

-- J.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment