navins
navins

Reputation: 3469

read upload file in play framework

I'm using play framework 2.1.0, upload files to play-app/upload folder.

then I run play 'start -Dhttp.port=80' to start server.

but when I upload a file to play-app/upload folder, it can not be access immediately.

if I stop the server and start again, then I can access the file.

How can I solve this problem? Thanks a lot.

ps, I route /upload as below:

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

Could it be that static files are loaded once? How can I solve it?

Upvotes: 0

Views: 2959

Answers (3)

navins
navins

Reputation: 3469

finally what I've solve it by adding access method in controller:

public static Result view(String filename) {
    File file  = new File(Play.application().path().getAbsolutePath() + "/upload/" + filename);
    return ok(file);
}

then, change route conf, you can access the files by the method.

BTW, if you are using play framework below 2.0, you may user:

    renderBinary(file, ContentType);

Upvotes: 1

user908853
user908853

Reputation:

I think what you need is to define a sort of Remote assets controller. Basically, once a file is uploaded, you put it in a folder that is outside your application's folder. Then, use a controller that will let you access it. Here is an example: http://www.jamesward.com/2012/08/08/edge-caching-with-play2-heroku-cloudfront

Here, James Ward creates a controller to access assets that are stored on cloudfront, what you need to do is to write a similar controller and replace the "content url" with the absolute path to your "Uploaded files directory".

Upvotes: 1

biesior
biesior

Reputation: 55798

Preferably create upload folder outside the application's folder and add it's full path like /home/navins/upload-folder/ in application.conf, then you'll be able to access it whole time, also you will be able to upload files there not only with app (ie, by FTP) without need of restarting.

Upvotes: 1

Related Questions