【发布时间】:2021-05-23 13:17:30
【问题描述】:
我有以下代码用于下载文件,然后重定向到DownloadController.scala 中Play Framework 下的感谢页面
def thankView = SecuredAction(WithProvider[AuthType](CredentialsProvider.ID)) {
implicit request: SecuredRequest[DefaultEnv, AnyContent] =>
Ok(downloadThankView(request.identity))
}
def download = SecuredAction(WithProvider[AuthType](CredentialsProvider.ID)) {
implicit request: SecuredRequest[DefaultEnv, AnyContent] =>
val futureMaybeFile = downloadService.generateDownload(request.identity.userID)
val maybeFile = Await.result(futureMaybeTempFile, 10 second)
maybeFile match {
case Some(file) =>
Ok.sendFile(
file,
fileName = _ => Some(file.getName),
onClose = () => {
file.delete()
// position (1) for placing the redirect, which doesn’t prevent the file download, but does not get executed
Redirect(routes.DownloadController.thankView())
}
).withHeaders(
"HttpResponse.entity.contentType" -> "text/txt",
"Content-Disposition" -> s"attachment; filename=${file.getName}"
)
// position (2) for placing the redirect which executes but prevents the file download
Redirect(routes.DownloadController.thankView())
case None =>
Redirect(routes.InfoController.view(Messages(“oops.there.was.error"), Messages("download.title")))
}
}
当我将Redirect(routes.DownloadController.thankView()) 行放在sendFile 的onClose 部分时,它不会被执行;当它放在sendFile 之后时,它会执行但会阻止文件被下载。
我在这里缺少什么?我该如何解决这个问题?
【问题讨论】:
标签: scala playframework