为什么我的formdata.append没有向服务器发送键值对?

时间:2014-11-03 03:09:24

标签: java javascript jquery apache-commons-fileupload

我认为它应该是非常简单的jquery代码,但我无法看到它将任何结果传递给服务器。我现在只想测试字符串,不需要文件!在服务器中,我只在请求中看到multipart / form-data,但是参数字段是{} .....请帮忙......

Host: localhost:8888
Connection: keep-alive
Content-Length: 143
Origin: http://localhost:8888
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryUCJkE7tIQ5AqLm74
Accept: */*
Referer: http://localhost:8888/
Accept-Encoding: gzip,deflate
Accept-Language: en-US,en;q=0.8,zh-CN;q=0.6

和请求有效负载类似于

------WebKitFormBoundary93G8teUQTA7hGmxn
Content-Disposition: form-data; name="action"

insert
------WebKitFormBoundary93G8teUQTA7hGmxn--

我使用java servlet作为服务器端,其中

String action = request.getParameter("action");

retrun null

以下是我的代码

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Distributed Storage System</title>
<script  src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

</head>
    <body>
    <div id="result"></div>
    <form id="form">
        <table>
          <tr>
            <td colspan="2" style="font-weight:bold;">Insert:</td>        
          </tr>
          <tr>        
            <td>Key:<input type="text" id="insertKey" name="key"/></td>
            <td>File:<input type="file" id="insertValue" name="file"/></td>
            <td><input type="button" value="Insert" id="insertValue" onClick="insert()"/></td>        
          </tr>
        </table>
    </form>
    <script type="text/javascript">
        function insert(){
            var data = new FormData();
            data.append('action','insert');
            //var file = $("#insertValue")[0].files[0];
            var xhr = new XMLHttpRequest();
            xhr.open( 'POST', '/distributedstorage', true );
            xhr.send( data );
        }

    </script>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

对于Content-Type: multipart/form-data,我们应该使用FileItem方法getFieldName() getString()来获取常规表单字段数据。
在这里,我从文档中提供了有关代码段的更多详细信息,使用Servelet

阅读ServletFileUpload中的所有表单字段数据

<强> Give a look into apache-docs for Processing the uploaded items

有关此处理的信息,请参阅下面的代码片段:

处理上传的表单元素包含常规字段和文件

// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = iter.next();

    if (item.isFormField()) {
        processFormField(item);
    } else {
        processUploadedFile(item);
    }
}

处理常规表单字段

// processFormField
if (item.isFormField()) {
    String name = item.getFieldName();
    String value = item.getString();
    ...
}

处理文件上传

// processUploadedFile
if (!item.isFormField()) {
    String fieldName = item.getFieldName();
    String fileName = item.getName();
    String contentType = item.getContentType();
    boolean isInMemory = item.isInMemory();
    long sizeInBytes = item.getSize();
    ...
}

请继续http://commons.apache.org/proper/commons-fileupload/using.html

中的其余代码