navins
navins

Reputation: 3469

Route to upload file in Play Framework 2.10

I upload files to /upload folder, then I want to directly access my files, like:

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:

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

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

Can somebody help me with this route? Thanks.

Upvotes: 1

Views: 1801

Answers (2)

navins
navins

Reputation: 3469

finnal, I got answer from playframework website, it not very obvious to find.. 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:

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. as:

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

in the html file, I should write as below:

@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. thanks TizianoPiccardi

playAssetsDirectories <+= baseDirectory / "foo"

Upvotes: 3

TizianoPiccardi
TizianoPiccardi

Reputation: 497

You should add this line in 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

Upvotes: 0

Related Questions