Play! upload images and make them available in production at /assets/images

淺唱寂寞╮ 提交于 2019-12-25 06:27:52

问题


From the docs:

When you package your application, all assets for the application, including all sub projects, are aggregated into a single jar, in target/my-first-app-1.0.0-assets.jar. This jar is included in the distribution so that your Play application can serve them. This jar can also be used to deploy the assets to a CDN or reverse proxy.

I got a few image uploads in my application, everything works fine in development but in production it gives me errors.

The thing is, I need those files in the public/images folder in order to display them on the page but it does not work in production.

How can I let the user upload images and also make them available at /public/images ?

Do I have to create another Assets path or something like that ?

note: Didn't post code because it's just a standard upload method from the docs.


回答1:


What I did:

I'm aware of the fact that this might be just a stupid workaround.

GET         /images/:name                               controllers.ImageController.show(name: String)

I created a simple action that serves the images like:

def show(name: String) = Action { implicit r =>
    val rootPath = Play.application.path
    val path = FileSystems.getDefault().getPath("/" + rootPath + "/public/images/");
    Logger.info("serving image: " + path.toString  + "/" + name )
    Ok.sendFile(new java.io.File(path.toString + "/" + name))
  }

Basically this whole operation will fail in development but it works in production.

After deployment, I created the folders manually e.g

 your_root/target/universal/stage/public/images

Now all the images are available at:

 /images/name_of_the_image

note: Play version = 2.4.x



来源:https://stackoverflow.com/questions/37089948/play-upload-images-and-make-them-available-in-production-at-assets-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!