Wednesday, June 22, 2011

Wicket Ajax Callback and FileUploadField

NextReports Server has an Upload action for different kind of reports.  Besides selecting report files and images, user has to enter a name for the report. We wanted to automatically set the name after selecting file (for lazy people), but this approach proved to be incorrect.

We had something like in the following snippet:
uploadField.add(new AjaxEventBehavior("onchange") {
               
     protected void onEvent(AjaxRequestTarget target) {
         Request request = RequestCycle.get().getRequest();
         String filename = request.getParameter("filename");
         String text = getModel().getObject().getName();
         if ((text == null) || "".equals(text.trim())) {
            int index = filename.lastIndexOf(".");
            if (index > 0) {
               filename = filename.substring(0,  index);
            } 
            getModel().getObject().setName(filename);                        
         }                    
         target.addComponent(name);
     }

     public CharSequence getCallbackUrl(boolean onlyTargetActivePage) {
         CharSequence callBackUrl = super.getCallbackUrl(onlyTargetActivePage);
         return callBackUrl + "&filename=' + this.value + '";
     }

}); 

Starting from IE8 and also in Chrome, but not yet in Firefox,  according to the specifications of HTML5, a file upload control should not reveal the real local path to the file you have selected, if you manipulate its value string with JavaScript. Instead, the string that is returned by the script, which handles the file information is c:\fakepath.

So, we have to give up using this functionality.

No comments: