Multipart file upload with access to the file upload stream

1,608 views
Skip to first unread message

Rich V

unread,
May 24, 2012, 1:37:01 PM5/24/12
to play-fr...@googlegroups.com
The out of the box multipart upload functionality in Play 2.0 allows access to the entire file after the upload is complete. The website I'm developing will require efficient uploads so this approach won't be appropriate for this specific case.  I've read much of the Play 2.0 documentation that's online and found that the best approach to handling this is to write a custom body parser. The ScalaBodyParser documentation page lists some interesting examples but the code for them is nowhere to be found:
A file body parser could store each chunk of bytes into a local file, and give a reference to the java.io.File as result (Iteratee[Array[Byte],File]).

A s3 body parser could push each chunk of bytes to Amazon S3 and give a the S3 object id as result (Iteratee[Array[Byte],S3ObjectId]).


I looked through the Play 2.0 source, specifically at the BodyParsers code and didn't find anything related to handling streams. I also searched Google extensively and couldn't find any examples. Can someone point me in the right direction for how to develop this, perhaps some sample code, a blog post, documentation, or github link? Any help is greatly appreciated. Thanks!

Joseph Burns

unread,
May 25, 2012, 8:29:18 AM5/25/12
to play-fr...@googlegroups.com
I'd also be interested in this if anyone is able to show a snippet.

evaneus

unread,
Jun 4, 2012, 10:41:01 AM6/4/12
to play-fr...@googlegroups.com
+1

Guillaume Bort

unread,
Jun 4, 2012, 10:55:18 AM6/4/12
to play-fr...@googlegroups.com
You can use `parse.multipartFormData(myCustomFilePartHandler)` to do
that. You are right, the default file parts handler store files on
disk as temporary files. But by defining your own
`myCustomFilePartHandler` you can change that.

You have to implement your own PartHandler[A]:

https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/play/api/mvc/ContentTypes.scala#L624

Where A is FilePart[B]:

https://github.com/playframework/Play20/blob/master/framework/src/play/src/main/scala/play/api/mvc/ContentTypes.scala#L141

B could be anything (like a reference to an S3 object)
> --
> You received this message because you are subscribed to the Google Groups
> "play-framework" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/play-framework/-/6SWFSmFGHdgJ.
>
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to
> play-framewor...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/play-framework?hl=en.



--
Guillaume Bort, http://guillaume.bort.fr
Message has been deleted

biesior

unread,
Jun 4, 2012, 11:03:52 AM6/4/12
to play-fr...@googlegroups.com
There is very interesting presentation by Sadek and Guillaume in some moment Sadek also describes how to ie. slow down the upload if needed using Iteratee, in gerneral  it's worth of watching from beginning to the end.

http://www.infoq.com/presentations/Play-I-ll-See-Your-Async-and-Raise-You-Reactive

David M.

unread,
Aug 22, 2012, 1:30:15 AM8/22/12
to play-fr...@googlegroups.com
Hello,

I have exactly the same requirement
I found this post (http://stackoverflow.com/questions/11468768/why-makes-calling-error-or-done-in-a-bodyparsers-iteratee-the-request-hang-in-p) that shows an example of BodyParser but for the moment it hasn't helped me achieve my goal (retrieving an InputStream I could pass to my thrid-party library)

Have you found a solution?
Reply all
Reply to author
Forward
0 new messages