Scala: upload file

เขียนต่อๆ มันส์

scala และ play framework upload file

ทำตามดังนี้

1 เขียน form html รับ upload file

<form action="/upload" method="post" class="form-horizontal">
    
Excel file
Upload
</div> </div> </form>

Screen Shot 2016-08-20 at 11.22.28 AM.png

2 map routes ไปหา controller ที่ไฟล์ routes

POST        /upload                             controllers.web.Controller.uploadFile

Screen Shot 2016-08-20 at 11.26.38 AM.png

3 ที่ controller จับยัดใน path ที่กำหนด

อันนี้ผมยัด excel มันเลยมีคำว่า excel

def uploadFile = Action(parse.multipartFormData) { request =>
  request.body.file("uploadFile").map { excel =>
    val filePath = s"./storage/uploaded/${excel.filename}"
    excel.ref.moveTo(new File(filePath))

    Ok(views.html.page.upload("success"))
  }.getOrElse {
    Ok(views.html.page.upload("fail"))
  }
}

Screen Shot 2016-08-20 at 11.27.28 AM

เย้เย!!!

About champillon

Enterprise Opensource Implementer
This entry was posted in Scala and tagged . Bookmark the permalink.

Leave a comment