Reputation: 1000
Say, if I have an excel file (size range from 0 ~ 100MB) which needs to be uploaded from client side to server, then it'll get parsed. How to do that with Akka-Http ? I read the doc, it seems they only support json and csv format.
Besides, from the view of server side, do I have to parse the file only after it has been uploaded or I can parse one data entry a time as long as it's available during the process of uploading ?
Upvotes: 0
Views: 871
Reputation: 17953
There is nothing special about an excel file as opposed to any other file type.
File Directive
There already exists directives to respond to get requests with a file:
import akka.http.scaladsl.server.Directives._
import java.io.File
import akka.http.scaladsl.model.ContentTypes
val easyRoute : Route =
getFromFile(File("/path/to/excelFile.xls"),
ContentTypes.`application/octet-stream`)
Manual Creation
You can use file io to create a Source
of the excel file as a ChunkStreamPart
:
import akka.stream.scaladsl._
import akka.http.scaladsl.model.HttpEntity.ChunkStreamPart
val excelFile = Paths get "example.xls"
val fileChunkSource : () => Source[ChunkStreamPart, _] =
() => FileIO fromPath excelFile
This file source can then be used to create an HttpEntity:
import akka.http.scaladsl.model.HttpEntity
val entity : () => HttpEntity =
() => HttpEntity(ContentTypes.`application/octet-stream`, fileSource())
Which can then be the entity of an HttpResponse
:
import akka.http.scaladsl.model.HttpResponse
val excelResponse : () => HttpResponse =
() => HttpResponse(entity = entity())
This response can be part of a standard route:
val route =
get {
path(/ "excelFile") {
complete(excelResponse())
}
}
Upvotes: 2