Skip to content

File Upload & Download

Upload routes extend FileUploadRoute and receive a Map<String, HttpBuffer> of uploaded files keyed by form-field name:

public class UploadHandler extends FileUploadRoute<UploadResponse, MyState> {
@Override
public LuxisPipeline<UploadResponse> handle(HttpStream<Map<String, HttpBuffer>, MyState> e) {
return e.complete(ctx -> {
int totalBytes = ctx.in().values().stream().mapToInt(b -> b.bytes().length).sum();
return HttpResult.success(new UploadResponse(totalBytes));
});
}
}

Register the upload route:

routesRegister.uploadFileRoute("/upload", Method.POST, appState, new UploadHandler());

Download routes return a DownloadFileResponse, which wraps an HttpBuffer body and a filename:

public class DownloadHandler implements FileDownloadRoute<String, MyState> {
@Override
public LuxisPipeline<DownloadFileResponse> handle(HttpStream<String, MyState> e) {
return e.complete(ctx ->
HttpResult.success(new DownloadFileResponse(HttpBuffer.fromString("file contents"), "report.csv")));
}
}

Register the download route with a content type:

routesRegister.downloadFileRoute("/download", Method.GET, appState, new DownloadHandler(), "text/csv");