weixin_33691817 2017-09-06 09:59 采纳率: 0%
浏览 121

groovy通过ajax获得formdata

I'm trying to transfer an image-file and corresponding information via ajax to a groovlet-server.

Problem: I can't get the data out of the HTTPServletRequest obect.

Here is the Javascript-Code that I use to transfer the data:

$("#submitButton").click( function(){
        if ( submitButtonCondition == true ) {

            //Gathering Data                
            var enabledValue = false;
            if ($("#activate").val()){
                enabledValue = true;
            }       
            var metadata = $("#metaTextarea").val();                                
            var inputFile = $("#fileInput")[0].files[0];

            // Creating FormData-Object filled with necessary Data
            var formData = new FormData();      
            formData.append('file', inputFile);
            formData.append('enabled', enabledValue);
            formData.append('metadata', metadata);

            // Sending FormData to Server
            $.ajax({
                type : 'POST',
                url : '/createNewEntry.groovy',
                contentType: false,
                processData: false,
                data: formData,
                success: function(resultData){  
                    console.log("Upload successful");
                },
                failure: function(resultData){
                    console.log("Upload failed");
                }
            });
        }
    }); 

The only way of verifying if data has been send has been accessing the attached reader of the request object: System.out.println(request.reader.text);

Output looks like this:

------WebKitFormBoundaryzNUfRksUAVW2ioCa
Content-Disposition: form-data; name="file"; filename="blatest.png"
Content-Type: image/png


------WebKitFormBoundaryzNUfRksUAVW2ioCa
Content-Disposition: form-data; name="enabled"

true
------WebKitFormBoundaryzNUfRksUAVW2ioCa
Content-Disposition: form-data; name="metadata"

asdfasdfasdf
------WebKitFormBoundaryzNUfRksUAVW2ioCa--

So apparently the data has been transferred? Still, I'm struggling to get information out of methods getParameter, getParameterMap, getParameterNames, getParameterValues which all give me no output.

  • 写回答

2条回答 默认 最新

  • weixin_33739646 2017-09-06 10:31
    关注

    you got on server side multipart request

    normally your request should be instanceof

    http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html

    and you can use methods:

    • Part getPart(java.lang.String name) Gets the Part with the given name.
    • java.util.Collection<Part> getParts()
    评论

报告相同问题?