So I researched and found out that Play's Production
mode has a different behavior when dealing with assets
than it does in Development
mode.
I have a site where the user uploads an image and the image is displayed immediately on the page after the upload. In development mode, the image displays fine. But in production mode, the image could not be found. I understand that in production mode, the code hasn't recognize the new file written.
A user with a similar issue wrote a solution but it is in Scala. I am writing in Java and don't know exactly what this user's solution is doing: Play! Framework: File not served after upload until play clean
It seems as though this solution is serving the file as a download? Because if it is, it's not what I need. I want to access the file to display it using html like this:
<img src='@routes.Assets.at("images/fileName")'></img>
What can I do to access newly uploaded asset file in Production
mode?
So finding out that you can't serve newly added files after Play has compile the code, the next best option is to use a front-end HTTP server like many have suggested. I ended up using nginx. Since I am only using nginx to serve files and nothing else, I'll post the steps that I took to get this to work.
I am using Mac so I installed Homebrew by calling $ brew install wget
in the terminal. Homebrew is good because you can install nginx by simply calling $ brew install nginx
I used this site to guide me through on how to modify nginx's config file: http://learnaholic.me/2012/10/10/installing-nginx-in-mac-os-x-mountain-lion/
Then I simply modify the config file from
location / {
root html;
index index.html index.htm;
}
to
location / {
root /Users/myName/playProject/public;
}
where public
refers to the folder in the Play project. I did it this way so that I don't have to rearrange my code. So now instead of
<img src='@routes.Assets.at("images/fileName")'></img>
I use
<img src='http://localhost:8080/images/fileName'></img>
Where port 8080 is my nginx server
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With