Play Framework - 仅从 Ok.sendFile(file) 获取文件内容
Play Framework - only getting file content from Ok.sendFile(file)
我的用户在我的 Play 应用程序中点击某个控制器时需要下载一个文件。我认为这可以解决问题:
def downloadFile = Action {
Ok.sendFile(new File("example.zip"))
}
但是好像只给出了文件的实际内容,并没有下载文件。谁能告诉我我做错了什么?
谢谢
试试这个:
def index = Action {
Ok.sendFile(
content = new java.io.File("/tmp/fileToServe.pdf"),
fileName = _ => "termsOfService.pdf"
)
}
它来自文档本身。
Now you don’t have to specify a file name since the web browser will
not try to download it, but will just display the file content in the
web browser window. This is useful for content types supported
natively by the web browser, such as text, HTML or images.
看到这个:https://www.playframework.com/documentation/2.0/ScalaStream
事实证明,我们使用的 REST 客户端会自动将文件直接转换为其内容,而不是让我们下载文件。从普通浏览器中点击它会按预期工作。
我的用户在我的 Play 应用程序中点击某个控制器时需要下载一个文件。我认为这可以解决问题:
def downloadFile = Action {
Ok.sendFile(new File("example.zip"))
}
但是好像只给出了文件的实际内容,并没有下载文件。谁能告诉我我做错了什么?
谢谢
试试这个:
def index = Action {
Ok.sendFile(
content = new java.io.File("/tmp/fileToServe.pdf"),
fileName = _ => "termsOfService.pdf"
)
}
它来自文档本身。
Now you don’t have to specify a file name since the web browser will not try to download it, but will just display the file content in the web browser window. This is useful for content types supported natively by the web browser, such as text, HTML or images.
看到这个:https://www.playframework.com/documentation/2.0/ScalaStream
事实证明,我们使用的 REST 客户端会自动将文件直接转换为其内容,而不是让我们下载文件。从普通浏览器中点击它会按预期工作。