Wicket - FileUploadField, Ajax and Preview

2019-07-26 22:07发布

问题:

Im using Wicket 1.5 and I need to build a component with a FileUploadField to load an image.

I need an Ajax behaviour to make a preview of image after selected it (without submiting the entire form).

Searching on Google, I found this Event that match when I select the file:

AjaxEventBehavior choose = new AjaxEventBehavior("onChange"){
      private static final long serialVersionUID = 1L;
      @Override
      protected void onEvent(AjaxRequestTarget target) {
        Request request = RequestCycle.get().getRequest();
      }
};

What I need is the stream of image to put in a little panel that required:

byte[] imgBytes

And obviously I need the same stream to fill a PropertyModel for DB storing.

Thanks

回答1:

You need to use either AjaxFormSubmitBehavior (will submit the entire form on the given event) or AjaxFormComponentUpdatingBehavior (will submit only the one form component. I'm not sure whether the latter works with file uploads, just give it a try. You can always use the former.

In the model of your FileUploadField you will find a (list of) FileUpload - look at the methods you get, there are input streams and other things available so you can do pretty much anything with the data.