我在DB中有一些文本数据,我希望按文件将这些数据输出给用户。我怎样才能做到这一点?现在我这样做:1。创建tmp文件。 2.将数据写入文件。 3.使用getFromFile()spray方法提供此文件。 没有1步和2步就可以做到这一点吗?
答案 0 :(得分:0)
您可以执行以下操作:
respondWithMediaType("text/plain") {
respondWithHeader(HttpHeaders.`Content-Disposition`.apply("attachment", Map("filename" -> ("text.txt")))) {
implicit val bufferMarshaller = BasicMarshallers.byteArrayMarshaller("text/plain")
autoChunk(ChunkSizeMagnet.fromChunkSize(5000000)) {
withRangeSupport() {
complete("file content".getBytes)
}
}
}
}