本文介绍了向客户端发送xls文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用喷涂开发一个休息api我需要从我的网络客户端下载一个带有报告的excel文件。

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

excel生成器方法准备就绪但是喷雾的getFromFile(fileFullPath)正在获得内部服务器错误

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

任何想法?

我的喷码:

(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


推荐答案

您代码的主要问题是 getFromFile(path) 对请求不做任何事情,而是返回一个新的函数 RequestContext =>单元从未被调用。一个解决方案可能是用 getFromFile(path)(ctx)替换该行。

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)
}

不知道为什么你的场景中得到 500内部服务器错误。控制台上什么都没有显示出什么问题?

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?

这篇关于向客户端发送xls文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-09 14:11