Spray send xls file to client

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 07:00:33

问题


I'm developing a rest api with spray I need to download from my web-client an excel file with a report.

The excel-generator method is ready but spray's "getFromFile(fileFullPath)" is getting "Internal server error"

Any ideas?

Here is my spray code:

(ctx: RequestContext) => {
   val actor = actorRefFactory.actorOf(Props(new Actor {
      def receive = {
        case GetAnualReport(year, generateExcel) =>
          val flujoActor = context.actorOf(Props[FlujoActor])
          flujoActor ! GetAnualReport(year, generateExcel)
        case ReporteResponse(path) =>
          println("FILE: "+path)
          getFromFile(path)
      }
   }))
actor ! GetAnualReport(year, true)
}

OUTPUT:

FILE: /tmp/flujocaja-reports-5627299217173924055/reporte-anual.xls
HTTP/1.1 500 Internal Server Error

回答1:


The main problem with your code is that getFromFile(path) doesn't do anything with the request but instead returns a new function RequestContext => Unit which is never called. One solution could be to replace that line with getFromFile(path)(ctx).

However, there's a better way how to deal asynchronous work before continuing with an inner route: use futures and one of the FutureDirectives. Here's an example roughly adapted to your use case:

onSuccess((flujoActor ? GetAnualReport(year, generateExcel)).mapTo[ReporteResponse]) { response =>
  getFromResource(response.path)
}

That said, I'm not sure why you get 500 Internal Server Error in your scenario. Is there nothing on the console hinting at what the problem is?



来源:https://stackoverflow.com/questions/22697168/spray-send-xls-file-to-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!