0

I am using cropit(http://scottcheng.github.io/cropit/) tool on client side to crop images. It crops images and returns image in 'data:image/png;base64' format. Now i need this to be sent to play framework.

header/payload preview:

------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

What should be the form mapping?

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

Once i have received the pollImageBase64 as string of base64, how do i use it and save the image in scala/play framework?

Api
  • 1,582
  • 1
  • 14
  • 16

1 Answers1

0

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

You should leave away the field "pollImageBase64" away and try to parse the content as multipart/form-data. To sum up :

1) bind your form without "pollImageBase64" field

2) Read the body of the request as multipart/form-data just as follow :

request.body.asMultipartFormData.file("pollImageBase64").map { picture => 
    val file = picture.ref.file //will get you a plain Java file
Community
  • 1
  • 1
Louis F.
  • 2,018
  • 19
  • 22
  • Well, i am not trying to upload image file. i have updated my question to show you the header preview of form being submitted – Api Dec 22 '15 at 22:33
  • Did you try to use `BodyParsers.parse.multipartFormData` parser ? Once you can read it, you can manipulate it as follows : http://stackoverflow.com/questions/469695/decode-base64-data-in-java – Louis F. Dec 22 '15 at 23:35