path("upload") { /* uploads a file to the database */
post {
entity(as[Multipart.FormData]) { (formData) =>
val uploadedUrlsFuture = formData.parts.map(_.entity.dataBytes).mapAsync(parallelism = 1)(part =>
part
.map(_.toArray)
.runFold(ByteString())((totalBytes, bytes) => totalBytes ++ bytes)
.map(_.toArray[Byte]).map(ResourceManager.saveFile(_)) /* TODO update this to a service */
).grouped(100).runWith(Sink.head)
complete(OK)
}
}
}Hi Lucian,
As discussed on gitter, I believe this is doable using the formFields directive for example:
formFields('json, 'file.as[StrictForm.FileData]) {
case (json: String, file: StrictForm.FileData) => // not tested though...
I agree that this should be a) very simple and b) well documented, which it isn't currently, thus I opened a ticket to improve this, please let us know of your progress or comments in there: https://github.com/akka/akka/issues/18471
As for limiting the maximal size of an upload, it’s currently already possible as you can use this cookbook pattern:
http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0/scala/stream-cookbook.html#Limit_the_number_of_bytes_passing_through_a_stream_of_ByteStrings
We’re contemplating if it can be made available as a simple to use directive, here’s the ticket (feel free to voice your opinion there or jump in and help us out :-)):
https://github.com/akka/akka/issues/18493
--
>>>>>>>>>> Read the docs: http://akka.io/docs/
>>>>>>>>>> Check the FAQ: http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>> Search the archives: https://groups.google.com/group/akka-user
---
You received this message because you are subscribed to the Google Groups "Akka User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email to akka-user+...@googlegroups.com.
To post to this group, send email to akka...@googlegroups.com.
Visit this group at http://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.
Hello everyone, for akka-http when handling a POST request, if I have my entity as a Multipart.FormData ,and I POST some JSON and a binary file, how can I split the two when I parse the formData ?