For this recipe, we will explore how to store and deliver files with Play applications by using MongoDB and GridFS. We will continue by adding to the previous recipe. As with the previous recipe, this recipe will be Scala only.
Let's take the following steps:
Run the
foo_scala
application with Hot-Reloading enabled:activator "~run"
Modify
foo_scala/app/controllers/WarehouseController.scala
by adding the following content:import java.text.SimpleDateFormat import play.api.libs.iteratee.Enumerator def upload = Action(parse.multipartFormData) { request => request.body.file("asset") match { case Some(asset) => { val gridFs = Warehouse.assets val uploadedAsset = gridFs.createFile(asset.ref.file) uploadedAsset.filename = asset.filename uploadedAsset.save() Ok("Asset is available at http://localhost:9000/warehouses/assets/%s".format(uploadedAsset.id)) ...