Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #184 from mnylen/examples-fileuploadsupport

Example of FileUploadSupport usage, scalatra-book/#91
  • Loading branch information...
commit 449e10ce53897d48bd37f708e77485adcfe0d136 2 parents 0703c7c + 53e5260
Ross A. Baker rossabaker authored
18 example/src/main/scala/Scalatra.scala
View
@@ -1,13 +1,15 @@
import org.scalatra._
-import org.eclipse.jetty.servlet.ServletHolder
class Scalatra extends LifeCycle {
override def init(context: ApplicationContext) {
- context.mount(new TemplateExample, "/")
- context.mount(new BasicAuthExample, "/auth")
- context.mount(new DocumentExample, "/docs")
- context.mount(new Servlet30ChatExample, "/chat_30")
-
+ context.mount(classOf[TemplateExample], "/")
+ context.mount(classOf[BasicAuthExample], "/auth")
+ context.mount(classOf[DocumentExample], "/docs")
+ context.mount(classOf[Servlet30ChatExample], "/chat_30")
+ context.mount(classOf[CookiesExample], "/cookies-example")
+ context.mount(classOf[FilterExample], "/")
+ context.mount(classOf[FileUploadExample], "/upload")
+
/*
// TODO: make work without web.xml, per servlet init parameters
val meteor = new ServletHolder(classOf[org.atmosphere.cpr.MeteorServlet])
@@ -15,8 +17,6 @@ class Scalatra extends LifeCycle {
meteor.setInitOrder(0)
context.mount(meteor, "/meteor/*") */
*/
-
- context.mount(new CookiesExample, "/cookies-example")
- context.mount(new FilterExample, "/")
+
}
}
61 example/src/main/scala/org/scalatra/FileUploadExample.scala
View
@@ -0,0 +1,61 @@
+package org.scalatra
+
+import javax.servlet.annotation.MultipartConfig
+import servlet.{SizeConstraintExceededException, FileUploadSupport}
+import xml.Node
+
+@MultipartConfig(maxFileSize = 3*1024*1024)
+class FileUploadExample extends ScalatraServlet with FileUploadSupport with FlashMapSupport {
+ object Template {
+ def page(content: Seq[Node]) = {
+ <html>
+ <head>
+ <title>File Upload</title>
+ </head>
+ <body>
+ <form action={url("/upload")} method="post" enctype="multipart/form-data">
+ <p>File to upload: <input type="file" name="file" /></p>
+ <p><input type="submit" value="Upload" /></p>
+ </form>
+
+ {content}
+ </body>
+ </html>
+ }
+ }
+
+ error {
+ case e: SizeConstraintExceededException =>
+ RequestEntityTooLarge(Template.page(
+ <p>The file you uploaded exceeded the 3 MB limit.</p>))
+ }
+
+ get("/") {
+ Template.page(
+ <p>
+ Upload a file using the above form. After you hit "Upload"
+ the file will be uploaded and your browser will start
+ downloading it.
+ </p>
+
+ <p>
+ The maximum file size accepted is 3 MB.
+ </p>)
+ }
+
+ post("/") {
+ fileParams.get("file") match {
+ case Some(file) =>
+ Ok(file.get(), Map(
+ "Content-Type" -> (file.contentType.getOrElse("application/octet-stream")),
+ "Content-Disposition" -> ("attachment; filename=\"" + file.name + "\"")
+ ))
+
+ case None =>
+ BadRequest(Template.page(
+ <p>
+ Hey! You forgot to select a file.
+ </p>))
+ }
+ }
+}
21 example/src/main/scala/org/scalatra/TemplateExample.scala
View
@@ -1,11 +1,9 @@
package org.scalatra
import scala.xml.{Text, Node}
-import org.apache.commons.io.IOUtils
-import fileupload.FileUploadSupport
import scalate.ScalateSupport
-class TemplateExample extends ScalatraServlet /*with FileUploadSupport*/ with FlashMapSupport with ScalateSupport {
+class TemplateExample extends ScalatraServlet with FlashMapSupport with ScalateSupport {
object Template {
@@ -128,23 +126,6 @@ class TemplateExample extends ScalatraServlet /*with FileUploadSupport*/ with Fl
layoutTemplate("index.scaml", "content"-> content)
}
- get("/upload") {
- Template.page("Scalatra: Session Example",
- <form method="post" enctype="multipart/form-data">
- Upload a file. Its contents will be displayed in the browser.<br />
- <label>File: <input type="file" name="file" /></label><br />
- <input type="submit" />
- </form>
- )
- }
-
- /*
- post("/upload") {
- contentType = "text/plain"
- fileParams.get("file") foreach { file => IOUtils.copy(file.getInputStream, response.getOutputStream) }
- }
- */
-
get("/flash-map/form") {
Template.page("Scalatra: Flash Map Example",
<span>Supports the post-then-redirect pattern</span><br />
Please sign in to comment.
Something went wrong with that request. Please try again.