如何在 akka http 中为通过 POST 完成的下载指定文件名
How to specify file name for a download done via POST in akka http
用户发送了一个 post 请求,然后基于那个 post 正文我创建了一个 Excel 文件 (.xlsx) 并想发回该文件,不存储该文件本身。
def writeAsync(out: OutputStream): Unit = {
Future {
val wb = new XSSFWorkbook
val sheet1: Sheet = wb.createSheet("sheet1");
val os = new ByteArrayOutputStream()
wb.write(os)
os.writeTo(out)
out.close()
wb.close()
}
}
...
pathPrefix("createAndDownloadExcel") {
post {
...
val generatedFileName = "customGeneratedFileName.xlsx" // <-- this name should the file name be like
val (out, source) = StreamConverters.asOutputStream().preMaterialize()
writeAsync(out)
complete(HttpEntity(ContentTypes.`application/octet-stream`, source))
}
}
响应包含 excel 内容,文件名:“createAndDownloadExcel”,但我希望它的文件名基于单独生成的文件名字.
该名称稍后将根据 POST 正文手动生成,因此 pathPrefix("fixedName.xlsx") 中的简单更改无法满足我的需要。
如何解决这个问题,能够为返回的 OutputStream 提供一个动态文件名?
"org.apache.poi" % "poi-ooxml" % "5.2.0"
尝试添加回复 header Content-Disposition.
The first parameter in the HTTP context is either inline (default value, indicating it can be displayed inside the Web page, or as the Web page) or attachment (indicating it should be downloaded; most browsers presenting a 'Save as' dialog, prefilled with the value of the filename parameters if present).
import akka.http.scaladsl.model.headers.ContentDispositionTypes.attachment
import akka.http.scaladsl.model.headers.`Content-Disposition`
....
respondWithHeader(`Content-Disposition`(attachment, Map("filename" -> "customGeneratedFileName.xlsx"))) {
complete(HttpEntity(ContentTypes.`application/octet-stream`, source))
}
用户发送了一个 post 请求,然后基于那个 post 正文我创建了一个 Excel 文件 (.xlsx) 并想发回该文件,不存储该文件本身。
def writeAsync(out: OutputStream): Unit = {
Future {
val wb = new XSSFWorkbook
val sheet1: Sheet = wb.createSheet("sheet1");
val os = new ByteArrayOutputStream()
wb.write(os)
os.writeTo(out)
out.close()
wb.close()
}
}
...
pathPrefix("createAndDownloadExcel") {
post {
...
val generatedFileName = "customGeneratedFileName.xlsx" // <-- this name should the file name be like
val (out, source) = StreamConverters.asOutputStream().preMaterialize()
writeAsync(out)
complete(HttpEntity(ContentTypes.`application/octet-stream`, source))
}
}
响应包含 excel 内容,文件名:“createAndDownloadExcel”,但我希望它的文件名基于单独生成的文件名字.
该名称稍后将根据 POST 正文手动生成,因此 pathPrefix("fixedName.xlsx") 中的简单更改无法满足我的需要。
如何解决这个问题,能够为返回的 OutputStream 提供一个动态文件名?
"org.apache.poi" % "poi-ooxml" % "5.2.0"
尝试添加回复 header Content-Disposition.
The first parameter in the HTTP context is either inline (default value, indicating it can be displayed inside the Web page, or as the Web page) or attachment (indicating it should be downloaded; most browsers presenting a 'Save as' dialog, prefilled with the value of the filename parameters if present).
import akka.http.scaladsl.model.headers.ContentDispositionTypes.attachment
import akka.http.scaladsl.model.headers.`Content-Disposition`
....
respondWithHeader(`Content-Disposition`(attachment, Map("filename" -> "customGeneratedFileName.xlsx"))) {
complete(HttpEntity(ContentTypes.`application/octet-stream`, source))
}