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

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()
    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效