播放Scala操作未收到多部分表单数据

时间:2015-09-28 07:58:15

标签: jquery scala multipartform-data playframework-2.4

我想使用Play Scala Framework将文件上传到我的网络服务器。但服务器正在响应Bad Request (400)。我在不同的Web服务器上托管了单独的客户端。 我已将文件上传操作设为minimul,如下所示:

  def upload() = Action.async(parse.multipartFormData) { request =>
    val body = request.body
    val optFile = body.files.headOption.map(_.ref.file)

    optFile match {
      case None => Future.successful(Ok("Missing Upload File").as("text/json"))
      case Some(file) => Future.successful(Ok("uploaded").as("text/json"))
    }
  }

jQuery ajax请求设置如下:

var frm = new FormData() ;
frm.append('file', $('#uploadfiles')[0].files[0]);
    $.ajax( {
        url: 'http://localhost:9000/notes/upload',
  data: frm,
  processData: false,
        method: 'post',
        crossDomain: true,
  xhrFields: {
    withCredentials: true
  },
  contentType: 'multipart/form-data',
        success: function ( uploadStatus ) {
            alert('uploaded');
        },
        error: function ( error ) {
            alert('error');
        }
    } );

更新请求是:

General
Remote Address:[::1]:9000
Request URL:http://localhost:9000/notes/upload
Request Method:POST
Status Code:400 Bad Request
Response Headers
view source
Access-Control-Allow-Credentials:true
Access-Control-Allow-Origin:http://localhost:3000
Content-Length:2148
Content-Type:text/html; charset=utf-8
Date:Mon, 28 Sep 2015 07:45:02 GMT
Vary:Origin
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:511
Content-Type:multipart/form-data
Cookie:connect.sid=s%3AWqi9VT3x3VctZ0_k1V0X5PWMJB0evrZG.8ux2doySJchVgqJC9tmt%2BOBMHCN%2FD1KBOXnfhGa%2BOfM
Host:localhost:9000
Origin:http://localhost:3000
Referer:http://localhost:3000/home
User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36
Request Payload
------WebKitFormBoundaryhrfHwiE7FJD7GgSo
Content-Disposition: form-data; name="file"; filename="README"
Content-Type: application/octet-stream


------WebKitFormBoundaryhrfHwiE7FJD7GgSo--

虽然请求标头的内容类型是multipart/form-data。我的动作是不允许上传文件。甚至不执行行动。 我在这里错过了什么或做错了吗?任何帮助都将非常值得赞赏。感谢

0 个答案:

没有答案