Skip to content

Instantly share code, notes, and snippets.

@reyman
Last active December 14, 2015 10:29
Show Gist options
  • Save reyman/5072477 to your computer and use it in GitHub Desktop.
Save reyman/5072477 to your computer and use it in GitHub Desktop.
class MyRepoServlet(val system: ActorSystem) extends ScalatraServlet with ScalateSupport with FileUploadSupport with FutureSupport with SlickSupport {
configureMultipartHandling(MultipartConfig(maxFileSize = Some(3 * 1024 * 1024), fileSizeThreshold = Some(1024 * 1024 * 1024)))
protected implicit def executor: ExecutionContext = system.dispatcher
def processFile(upload: FileItem) = {
val filePath: String = "/tmp/"
println(">> " + upload.getName)
try {
val file: File = new File(filePath + upload.getName)
upload.write(file)
} catch {
case e: IOException ⇒ println("Error " + e)
}
}
get("/uploadMole") {
contentType = "text/html"
new AsyncResult() {
val is = Future {
ssp("/uploadMole", "body" -> "Please upload an om file below!")
}
}
}
post("/uploadMole") {
contentType = "text/html"
val x = new AsyncResult() {
val is = Future {
try {
val document: FileItem = fileParams("file")
println("file name: " + document.name)
processFile(document)
} catch {
case e: IOException ⇒ println("IO Error " + e)
case s: SecurityException ⇒ println("Security Error " + s)
}
}
}
}
<form name="data" action="/uploadMole" method="POST" enctype="multipart/form-data">
<input type="File" name="file"><br>
<input type="Submit">
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment