如何使用 Scala 在 Play!2 中提供上传的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11451246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 04:21:12  来源:igfitidea点击:

How to serve uploaded files in Play!2 using Scala?

scalaplayframeworkplayframework-2.0

提问by Henry Henrinson

I'm trying to allow users to upload photos to the server and then view them. Uploading happens as described in this guide. Here is the code:

我正在尝试允许用户将照片上传到服务器然后查看它们。按照本指南中的描述进行上传。这是代码:

def upload = Action(parse.multipartFormData) { request =>
  request.body.file("picture").map { picture =>
    import java.io.File
    val filename = picture.filename 
    val contentType = picture.contentType
    picture.ref.moveTo(new File("/tmp/picture"))
    Ok("File uploaded")
  }.getOrElse {
    Redirect(routes.Application.index).flashing(
      "error" -> "Missing file"
    )
  }
}

It is unclear to me how to serve the uploaded images back to users that want to see them. Right now I am hosting the server on my own machine, so the code snippet from the guide writes the files to my D: drive, which isn't (and shouldn't be) available from the Internet. As far as I can see there are 2 options:

我不清楚如何将上传的图像提供给想要查看它们的用户。现在我在自己的机器上托管服务器,因此指南中的代码片段将文件写入我的 D: 驱动器,该驱动器不能(也不应该)从 Internet 上获得。据我所知,有两个选项:

  1. Store the photos under the /public folder in my project (the one that is dedicated to assets). See here: http://www.playframework.org/documentation/2.0/Assets

  2. Write my own controller that servs images form custom locations from my drive.

  1. 将照片存储在我的项目(专用于资产的文件夹)中的 /public 文件夹下。请参阅此处:http: //www.playframework.org/documentation/2.0/Assets

  2. 编写我自己的控制器,从我的驱动器中提供自定义位置的图像。

For 1, I'm not sure if that is the purpose of assets. For 2, I have no idea how to write such a controller.

对于 1,我不确定这是否是资产的目的。对于 2,我不知道如何编写这样的控制器。

采纳答案by Marius Soutier

2.0.3 will feature an external Assets controllerwhich might be (mis)used for this. Writing such a controller is no magic though, you have predefined folder where all your uploads are saved, and that's where you read them from. In the database you save the (unique) file name.

2.0.3 将包含一个可能(误)用于此的外部资产控制器。编写这样的控制器并不神奇,你有一个预定义的文件夹,你的所有上传文件都保存在那里,这就是你从中读取它们的地方。在数据库中保存(唯一的)文件名。

A different approach would be to save the uploaded files in the database. We do this with GridFSin MongoDB. A custom controller serves them back to the user. This way your data is stored in one central place, which also makes backups and recoveries simpler.

另一种方法是将上传的文件保存在数据库中。我们使用MongoDB 中的GridFS来实现这一点。自定义控制器将它们返回给用户。通过这种方式,您的数据将存储在一个中央位置,这也使备份和恢复变得更加简单。

回答by Eugene Platonov

The simple example is

简单的例子是

def index = Action {
  Ok.sendFile(new java.io.File("/tmp/fileToServe.pdf"))
}

there is "Serving files" section at https://www.playframework.com/documentation/2.4.x/ScalaStream#Serving-fileswhich explains how to serve files

https://www.playframework.com/documentation/2.4.x/ScalaStream#Serving-files 上有“服务文件”部分,它解释了如何提供文件

回答by Peanut

You can add a new route like this:

您可以像这样添加新路线:

GET     /myFiles/*file               controllers.Assets.at(path="/tmp", file)