【问题标题】:akka-http. Generate file and send to client阿卡-http。生成文件并发送给客户端
【发布时间】:2017-08-22 23:42:13
【问题描述】:

我需要将字符串保存到文本文件并发送到浏览器。我的路线:

def genFileRoute: Route =
    path("someresource" / "generate") {
        get {
            complete( genFile )
        }
    }

但是我的生成器有问题:

def genFile = {
    val str = "Hi all! I am a string in future text file"

     // Need to write some code here :(
     // Something like 'php://output' with headers, but for akka.
}

如何在不使用文件系统的情况下创建内存中的文本文件file.txt?如何发送到客户端浏览器(下载)?

P.S.对不起,我的英语不是我的母语。

【问题讨论】:

    标签: scala akka-http


    【解决方案1】:

    您想为此使用Content-Disposition 标头 - 它允许您设置发送到浏览器的文件名。根本不需要创建文件;在您的示例中,该函数可以返回一个纯字符串。

    def genFile = {
        val str = "Hi all! I am a string in future text file"
    
        str
    }
    

    路线:

    path("someresource" / "generate") {
      get {
        respondWithHeader(`Content-Disposition`(
            ContentDispositionTypes.attachment, Map("filename" -> "somefile.txt"))) {
          complete(genFile)
        }
      }
    }
    

    【讨论】:

    • 未来的灵魂:import akka.http.scaladsl.model.headers._
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-17
    相关资源
    最近更新 更多