播放Scala MultipartFormData如何获得多个请求值

时间:2015-09-17 00:22:26

标签: scala playframework

Scala行动:

def uploadFile = Action(parse.multipartFormData) { request =>
request.body.file("uploadedFile").map { 
video =>

  val videoFilename = video.filename
  val contentType = video.contentType.get


 amazonS3Client.putObject(bucketName, "NEED VALUE OF FILENAME FROM REQUEST", video.ref.file)


}.getOrElse {
  Redirect(routes.Application.smighties)
}
Ok("File has been uploaded2")

}

调用上面Scala方法的控制器(coffeescript):

    fd = new FormData
    fd.append 'uploadedFile', theFile
    fd.append 'fileName', fileName

    xhr = new XMLHttpRequest
    xhr.open 'POST', '/uploadFile'
    xhr.send fd

我能够获得" uploadedFile"来自Action中的formData请求。但是你怎么得到" uploadedFile"和" fileName"要求?在上面的操作中,我需要将文件名传递给amazon,使最终上传的图像名称与原始文件名不同。

谢谢你,

2 个答案:

答案 0 :(得分:0)

由于您使用了正文解析器parse.multipartFormData,因此您的请求正文为MultipartFormData[Files.TemporaryFile] scaladoc

多部分表单数据的其他部分可在dataParts中找到:

val filename: Option[String] = for {
    parts <- request.body.dataParts.get("fileName")
    first <- parts.headOption
} yield first

// or unsafely

val filename: String = request.body.dataParts("fileName").head

答案 1 :(得分:0)

为什么不追加正常Play form

val myForm = Form(single("fileName" -> nonEmptyText))

然后输入uploadFile方法

myForm.bindFromRequest.fold(
    e  => BadRequest("manage error here"),
    fn => {
         // you current function here, filename will be stored as string in var `fn`
    }
)