gwtgwtupload

Where to call an Action when user removes an uploaded image in GWTUpload?


I am using GWTUpload , the library is in here https://code.google.com/p/gwtupload/

The Example code at client side found on that website has this structure:

 // Attach an image to the pictures viewer
private OnLoadPreloadedImageHandler showImage = new OnLoadPreloadedImageHandler() {
    public void onLoad(PreloadedImage image) {
      //showImageFlowPanel code solution 1
      image.setWidth("75px");
      showImageFlowPanel.add(image);
    }
};


private IUploader.OnFinishUploaderHandler onFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() {
      public void onFinish(IUploader uploader) {
        if (uploader.getStatus() == Status.SUCCESS) {

          new PreloadedImage(uploader.fileUrl(), showImage);

          UploadedInfo info = uploader.getServerInfo();

          String headShotImageUrl="http://"+Window.Location.getHost()+"/" +"images/uploaded/"+info.message;
          //headShotImage code solution 2
          if(!"".equals(headShotImageUrl) && UriUtils.isSafeUri(headShotImageUrl)){
              headShotImage.setUrl(UriUtils.fromString(headShotImageUrl));
          }



        }
      }
    };

The example uses showImageFlowPanel (solution 1) to store the image but I want to store the image inside headShotImage which take the url after user uploaded image successfully, see the headShotImage (solution 2) code above.

Ok, the headShotImage code work fine but I dont know how to remove it when users remove the image. If I use showImageFlowPanel as in the solution 1 then the program remove the image automatically for me and I do not need to do anything.

So my question is "Where to call an Action when user removes an uploaded image in GWTUpload?"


Solution

  • You have to use setOnCancelUploaderHandler. Take a look to this code took from the demo.

    // When the user clicks a cancel button we get an event
    uploader.addOnCancelUploadHandler ( 
      new IUploader.OnCancelUploaderHandler() {
        public void onCancel(IUploader uploader) {
          for (String iname : uploader.getServerMessage().getUploadedFieldNames()) {
            // loadedImages is an temporary table where we are adding all uploaded files
            // indexed by field name
            Widget w = loadedImages.get(iname);
            if (w != null) {
              w.removeFromParent();
            loadedImages.remove(uploader.getInputName());
           }
         }
       }
    });