fileUpload
Signature¶
def fileUpload(fieldName: String): Directive1[(FileInfo, Source[ByteString, Any])]
Description¶
Simple access to the stream of bytes for a file uploaded as a multipart form together with metadata about the upload.
If there is no field with the given name the request will be rejected. If there are multiple file parts with the same name, the first one will be used and the subsequent ones ignored.
Note
This directive will only upload one file with a given name. To upload multiple files with the same name you should use the fileUploadAll directive, though all files will be buffered to disk, even if there is only one.
Example¶
source
// adding integers as a service
val route =
extractRequestContext { ctx =>
implicit val materializer = ctx.materializer
fileUpload("csv") {
case (metadata, byteSource) =>
val sumF: Future[Int] =
// sum the numbers as they arrive so that we can
// accept any size of file
byteSource.via(Framing.delimiter(ByteString("\n"), 1024))
.mapConcat(_.utf8String.split(",").toVector)
.map(_.toInt)
.runFold(0) { (acc, n) => acc + n }
onSuccess(sumF) { sum => complete(s"Sum: $sum") }
}
}
// 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" -> "primes.csv")))
Post("/", multipartForm) ~> route ~> check {
status shouldEqual StatusCodes.OK
responseAs[String] shouldEqual "Sum: 178"
}
sourceimport static org.apache.pekko.http.javadsl.server.Directives.extractRequestContext;
import static org.apache.pekko.http.javadsl.server.Directives.fileUpload;
import static org.apache.pekko.http.javadsl.server.Directives.onSuccess;
final Route route =
extractRequestContext(
ctx -> {
return fileUpload(
"csv",
(metadata, byteSource) -> {
// sum the numbers as they arrive
CompletionStage<Integer> sumF =
byteSource
.via(Framing.delimiter(ByteString.fromString("\n"), 1024))
.mapConcat(bs -> Arrays.asList(bs.utf8String().split(",")))
.map(s -> Integer.parseInt(s))
.runFold(0, (acc, n) -> acc + n, ctx.getMaterializer());
return onSuccess(sumF, sum -> complete("Sum: " + sum));
});
});
Map<String, String> filenameMapping = new HashMap<>();
filenameMapping.put("filename", "primes.csv");
org.apache.pekko.http.javadsl.model.Multipart.FormData multipartForm =
Multiparts.createStrictFormDataFromParts(
Multiparts.createFormDataBodyPartStrict(
"csv",
HttpEntities.create(
ContentTypes.TEXT_PLAIN_UTF8, "2,3,5\n7,11,13,17,23\n29,31,37\n"),
filenameMapping));
// test:
testRoute(route)
.run(
HttpRequest.POST("/")
.withEntity(multipartForm.toEntity(BodyPartRenderer.randomBoundaryWithDefaults())))
.assertStatusCode(StatusCodes.OK)
.assertEntityAs(Unmarshaller.entityToString(), "Sum: 178");
1.0.1