如何在play框架中解析和使用base64编码的图像?

时间:2015-12-21 22:30:27

标签: java scala playframework

我在客户端使用cropit(http://scottcheng.github.io/cropit/)工具裁剪图像。它以“data:image / png; base64”格式裁剪图像并返回图像。现在我需要将它发送到播放框架。

标头/有效负载预览:

------WebKitFormBoundaryXnMMA8dXWah4PjT9
Content-Disposition: form-data; name="question"

test new poll?
 ------WebKitFormBoundaryXnMMA8dXWah4PjT9
    Content-Disposition: form-data; name="pollImageBase64"

    data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaQAAAExCAYAAAAz59bFAAAgAElEQVR4Xuy9B3Qs53Um+FVVdyN3N3J6iZmi+PgeSTFJtKWRaK9lW+OdXWssUZGUFWdndz1nnY6PPWMfWyOtPeNsS7IpM0mWf

表格映射应该是什么?

val newPollForm = Form(
    mapping(
       "question" -> nonEmptyText, 
      "pollImageBase64" -> optional(text) //doesn't work and returns none
    )(NewPollItem.apply)(NewPollItem.unapply)
  )

一旦我收到pollImageBase64作为base64的字符串,我如何使用它并将图像保存在scala / play框架中?

1 个答案:

答案 0 :(得分:0)

根据:How to get the upload file with other inputs in play2?

你应该远离字段“pollImageBase64”并尝试将内容解析为multipart / form-data。总结一下:

1)绑定没有“pollImageBase64”字段的表单

2)将请求正文作为multipart / form-data读取,如下所示:

request.body.asMultipartFormData.file("pollImageBase64").map { picture => 
    val file = picture.ref.file //will get you a plain Java file