简体   繁体   English

在Play Framework 2.10中上传文件的路线

[英]Route to upload file in Play Framework 2.10

I upload files to /upload folder, then I want to directly access my files, like: 我将文件上传到/upload文件夹,然后我想直接访问我的文件,如:

http://localhost/upload/xxx.jpg

when I add routes as below: 当我添加如下路线时:

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

It causes another error: 它会导致另一个错误:

not enough arguments for method at: (path: String, file: String)play.api.mvc.Call. Unspecified value parameter file.

<link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">

Then, after I change @routes.Assets.at("stylesheets/main.css") to @routes.Assets.at("stylesheets/", "main.css") , there is another error: 然后,在我将@routes.Assets.at("stylesheets/main.css")更改为@routes.Assets.at("stylesheets/", "main.css") ,还有另一个错误:

[MatchError: (stylesheets/,main.css) (of class scala.Tuple2)]

(path: @unchecked, file: @unchecked) match {

Can somebody help me with this route? 有人可以帮我这条路吗? Thanks. 谢谢。

finnal, I got answer from playframework website, it not very obvious to find.. http://www.playframework.com/documentation/2.0.4/Assets finnal,我从playframework网站得到了答案,找不到很明显.. http://www.playframework.com/documentation/2.0.4/Assets

from this page: 从这个页面:

However, if you define two mappings for the Assets.at action, like this: 但是,如果为Assets.at操作定义两个映射,如下所示:

GET  /javascripts/*file        controllers.Assets.at(path="/public/javascripts", file)
GET  /images/*file             controllers.Assets.at(path="/public/images", file)

Then you will need to specify both parameters when using the reverse router: 然后,在使用反向路由器时,您需要指定两个参数:

<script src="@routes.Assets.at("/public/javascripts", "jquery.js")"></script>

<image src="@routes.Assets.at("/public/images", "logo.png")">

but this may not solve my problem yet, it turn out to appear the second error mention in the question. 但这可能无法解决我的问题,结果却出现了问题中的第二个错误提及。

Be Careful, check the path param, it must be the same as you described in routes file. 小心,检查path参数,它必须与您在路径文件中描述的相同。 as: 如:

when I set: GET /public/*file controllers.Assets.at(path="/public", file) 当我设置: GET /public/*file controllers.Assets.at(path="/public", file)

in the html file, I should write as below: 在html文件中,我应该写如下:

@routes.Assets.at("/public", "stylesheets/main.css")


besides, if you use another folders, like /upload, adding below code in project/Build.scala in play.Project is essential. 此外,如果您使用其他文件夹,例如/ upload, project/Build.scalaplay.Project中的project/Build.scala中添加以下代码是必不可少的。 thanks TizianoPiccardi 谢谢TizianoPiccardi

playAssetsDirectories <+= baseDirectory / "foo"

You should add this line in project/Build.scala : 你应该在project / Build.scala中添加这一行:

  val main = play.Project(appName, appVersion, appDependencies).settings(
    // Add your own project settings here    
    playAssetsDirectories <+= baseDirectory / "upload"
  )

More info: https://github.com/playframework/Play20/wiki/Assets 更多信息: https//github.com/playframework/Play20/wiki/Assets

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM