Play Upload File using Restful API

685 views
Skip to first unread message

Roshan Sharma

unread,
Aug 5, 2013, 8:18:58 AM8/5/13
to play-fr...@googlegroups.com
I am new to play and rest. I am trying to implement restful file upload. Someone please provide me basic information regarding how should I request the API and how to upload from backend. What will be the request and how can it be stored in a physical path. 
Thank you in advance.

Roshan Sharma

unread,
Aug 15, 2013, 12:39:48 AM8/15/13
to play-fr...@googlegroups.com
Anyone? Please advice.

Jason Pearson

unread,
Aug 15, 2013, 12:54:02 AM8/15/13
to play-fr...@googlegroups.com
Are you using Java or Scala, and what version?


If you're really new to Play! I would suggest going through the other documentation about controllers, routes, and views to get going before you jump straight into handing multipart form data.


On Wed, Aug 14, 2013 at 10:39 PM, Roshan Sharma <rosh...@gmail.com> wrote:
Anyone? Please advice.

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Roshan Sharma

unread,
Aug 15, 2013, 8:09:22 AM8/15/13
to play-fr...@googlegroups.com
Thank you Jason for reply but I have gone through it. I am using scala 2.10.1. The application is RESTful (I dont have client) and I need to know how to upload and test. For testing I am using Chrome Poster.


--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/VTlRXOQgkm4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.

For more options, visit https://groups.google.com/groups/opt_out.



--
--
Roshan Sharma

Jason Pearson

unread,
Aug 21, 2013, 4:02:32 PM8/21/13
to play-fr...@googlegroups.com
Sorry for the lack of response, I'm on vacation.  Also what version of Play are you using?

Roshan Sharma

unread,
Aug 23, 2013, 5:24:53 AM8/23/13
to play-fr...@googlegroups.com
Its play 2.1.1

Puneet Jain

unread,
Oct 21, 2013, 6:58:54 PM10/21/13
to play-fr...@googlegroups.com
Hi Roshan,

Did you find the solution to your problem. I need to do the same. Please let me know the solution you found?

Thanks,
Puneet

Roshan Sharma

unread,
Oct 24, 2013, 2:52:10 AM10/24/13
to play-fr...@googlegroups.com
First I converted multipart form data to bytes:

def multipartFormDataAsBytes: BodyParser[MultipartFormData[Option[(Array[Byte], String, String)]]] = parse.multipartFormData(handleFilePartAsByteArray)

  def handleFilePartAsByteArray: BodyParsers.parse.Multipart.PartHandler[MultipartFormData.FilePart[Option[(Array[Byte], String, String)]]] = {
    parse.Multipart.handleFilePart {

      case parse.Multipart.FileInfo(partName, filename, contentType) =>
        Iteratee.fold[Array[Byte], ByteArrayOutputStream](
          new ByteArrayOutputStream()) { (os, data) =>
            os.write(data)
            os
          }.mapDone { os =>
            os.close()
            Some(os.toByteArray, filename, contentType.get)
          }
    }
  }


In my controller method with above parser, I did this:

     val params = request.body.asFormUrlEncoded
    
    request.body.files(0).ref match {
      case Some((data, fileName, contentType)) => {
        //your logic here
     }
      case _ => NotFound
    }

I dont know if its the best way but it was working for me and I didnt want to spend more time on this.
Reply all
Reply to author
Forward
0 new messages