Wicket Drag and drop functionality for adding an image

后端 未结 2 2060
野的像风
野的像风 2021-02-10 08:07

I\'m making a wicket app that can manage some options for a cashdesk app. One of the options is to change the image of a selected Product.

2条回答
  •  情深已故
    2021-02-10 08:29

    Based on your code I wrote my custom Ajax behavior. I simplified it however, it is working for me without dragging hack.

    AbstractFileDropAjaxBehavior.java

    public abstract class AbstractFileDropAjaxBehavior extends AbstractDefaultAjaxBehavior
    {
    
        @Override
        protected void respond(final AjaxRequestTarget target)
        {
            RequestCycle requestCycle = RequestCycle.get();
    
            StringValue data = requestCycle.getRequest().getRequestParameters().getParameterValue("data");
            String[] base64Data = data.toString().split(";");
            String fileName = base64Data[0].substring(base64Data[0].indexOf(':') + 1, base64Data[0].length());
            String dataType = base64Data[1].substring(base64Data[1].indexOf(':') + 1, base64Data[1].length());
            String binaryData = base64Data[2].substring(base64Data[2].indexOf(',') + 1, base64Data[2].length());
            byte[] rawData = DatatypeConverter.parseBase64Binary(binaryData);
    
            processFile(fileName, dataType, rawData);
        }
    
        @Override
        protected void onComponentTag(ComponentTag tag) {
            tag.put("my:dropcontainer.callback", getCallbackUrl().toString());
    
            tag.put("ondragover", "return false;");
            tag.put("ondrop", "return AbstractFileDropAjaxBehavior_upload(event, '#" + tag.getId() + "');");
            tag.put("ondragenter", "$('#" + tag.getId() + "').addClass('dropover')");
            tag.put("ondragleave", "$('#" + tag.getId() + "').removeClass('dropover')");
        }
    
        @Override
        public void renderHead(Component component, IHeaderResponse response) {
            super.renderHead(component, response);
    
            response.render(OnDomReadyHeaderItem.forScript("jQuery.event.props.push('dataTransfer');"));
    
            response.render(JavaScriptContentHeaderItem.forScript("function AbstractFileDropAjaxBehavior_upload(e, selector) {\n" + 
                    "   var files = e.dataTransfer.files;\n" + 
                    "   \n" + 
                    "   $.each(files, function(i, file) {\n" + 
                    "       var reader = new FileReader();\n" + 
                    "       \n" + 
                    "       reader.onload = function(input) {\n" + 
                    "           var fileName = \"fileName:\" + file.name + \";\";\n" + 
                    "           var base64data = input.target.result;\n" + 
                    "           \n" + 
                    "           Wicket.Ajax.post({\n" + 
                    "               \"u\": $(selector).attr('my:dropcontainer.callback'),\n" + 
                    "               \"ep\": {\n" + 
                    "                   \"data\" : fileName + base64data,\n" + 
                    "               },\n" + 
                    "           });\n" + 
                    "           \n" + 
                    "       };\n" + 
                    "\n" + 
                    "       reader.readAsDataURL(file);\n" + 
                    "   });\n" + 
                    "   \n" + 
                    "   $(selector).removeClass('dropover');\n" + 
                    "   \n" + 
                    "   return false;   \n" + 
                    "}\n", "AbstractFileDropAjaxBehavior-script"));
        }
    
        protected abstract void processFile(String fileName, String dataType, byte[] rawData);
    
    }
    

提交回复
热议问题