Streams the contents of all files uploaded in a multipart form into files on disk and provides a list of each file and metadata about the upload.
If there is an error writing to disk the request will be failed with the thrown exception. If there is no field with the given name the request will be rejected.
source def tempDestination(fileInfo:FileInfo):File=Files.createTempFile(fileInfo.fileName,".tmp").toFile
val route =
storeUploadedFiles("csv", tempDestination){ files =>
val finalStatus = files.foldLeft(StatusCodes.OK){case(status,(metadata, file))=>// do something with the file and file metadata ...
file.delete()
status
}
complete(finalStatus)}// tests:
val multipartForm =Multipart.FormData(Multipart.FormData.BodyPart.Strict("csv",HttpEntity(ContentTypes.`text/plain(UTF-8)`,"2,3,5\n7,11,13,17,23\n29,31,37\n"),Map("filename"->"primesA.csv")),Multipart.FormData.BodyPart.Strict("csv",HttpEntity(ContentTypes.`text/plain(UTF-8)`,"41,43,47\n53,59,6167,71\n73,79,83\n"),Map("filename"->"primesB.csv")))Post("/", multipartForm)~> route ~> check {
status shouldEqual StatusCodes.OK
}