How to store/retrieve images on local file systems?

1,578 views
Skip to first unread message

David Lin

unread,
Sep 4, 2013, 5:20:22 PM9/4/13
to play-fr...@googlegroups.com
I want to create a web application that allows users to upload multiple images and display them. (Something like instagram, photobucket, etc)

I read multiple resources online and learned that storing images on databases is a bad practice. The way to do it is storing those images in a file systems like Amazon S3. I'm a high school student so I don't have money for those. Neither do I know what they mean. I just want to test it using my local file systems. For examples, uploading them into a folder on my computer and retrieve them. I already have a table for users and their id. I'm thinking of creating another table for images and link those images to the user. Am I on the right track? How do I upload and retrieve images from my local file systems with play framework? 

I mostly self-learned and have very minimum knowledge about coding so if you provide some simple code and explanation, it would be great. 

Thank you so much!

Yann Simon

unread,
Sep 5, 2013, 2:33:25 AM9/5/13
to play-fr...@googlegroups.com
Hi David,

I am not sure that storing files on the file system is a better idea than storing files as blobs in the database.
What do you do if you have more than one instance of your play application and one databasse?
Synchronize all file systems for all play servers?
Use a remote file system?

Nevertheless, to achieve what you want, if you are using scala:
with a body parser, you can save a file to the file system:

To send a file stored on the file system:
    Ok.sendFile(fileToServe)

Cheers,
Yann



2013/9/4 David Lin <davidl...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "play-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Jason Pearson

unread,
Sep 5, 2013, 9:04:29 AM9/5/13
to play-fr...@googlegroups.com

Also Amazon has a free tier for most of its services (up to 5 GB for s3). You can also put your test apps on Heroku to experiment with deployment, works well with Play 2.x.

David Lin

unread,
Sep 5, 2013, 5:13:05 PM9/5/13
to play-fr...@googlegroups.com
Hi Yann, 

I'm using Java so I looked at this: http://www.playframework.com/documentation/2.0/JavaBodyParsers. But I still don't understand how it helps? 
Perhaps my question wasn't clear. Let me explain it again using an example. 

Say there is an image called "a.jpg" located at my C:/ drive
When I click on "browse", a window popup and I can go to C:/ to select that image (Just like how we upload photos to Facebook, etc)
After I select the image and click "Upload", this image will be saved to "C:/playframework/myapp/user1234/a.jpg"
When I click on "View my photos", it will retrieve all the images uder user1234/ folder and display them.

Does my approach make sense or it's completely off? Are there any similar examples on how this is being done? Does playframework have any methods that support this? Some example code would be very helpful!

Thanks,
David. 

Turar

unread,
Dec 13, 2013, 12:25:42 AM12/13/13
to play-fr...@googlegroups.com
I have a similar need to serve local images. I used the OK.sendFile example with "inline = true" argument as described here: http://www.playframework.com/documentation/2.2.x/ScalaStream

However, the browser is forcing the download of the JPG file, instead of simply displaying it inline. What am I missing? Previously, I used to work-around this issue by just embedding the image into template view using <img> tag, but now this is causing problems with various jQuery plugins I'd like to start using.

Thanks in advance!
Reply all
Reply to author
Forward
0 new messages