Another way to send files to the server is to use Ajax to upload the file asynchronously in a form. In this case the request body will not have been encoded as multipart/form-data, but will just contain the plain file content.

In this case we can just use a body parser to store the request body content in a file. For this example, let’s use the temporaryFile body parser:

If you want to handle the file upload directly without buffering it in a temporary file, you can just write your own BodyParser. In this case, you will receive chunks of data that you are free to push anywhere you want.

If you want to use multipart/form-data encoding, you can still use the default mutipartFormData parser by providing your own PartHandler[FilePart[A]]. You receive the part headers, and you have to provide an Iteratee[Array[Byte], FilePart[A]] that will produce the right FilePart.