Scala LiftWeb: Upload File using AJAX


Liftweb provides AJAX functionality for all html elements except file upload using Scala .
But there is a way to upload file using AJAX by binding form as AJAX form .

1) add below lines in your html .

<div class="lift:FileSnippet.uploadFile">
Upload File : <input name="upload" type="file"></input>
<input  type="submit" name="submit" value="Upload"/>
</div>

2)

class FileSnippet {
      def uploadFile(form: NodeSeq): NodeSeq = {

          def handleFile():JsCmd = {
            // Add Your JavaScript Code
          }

          var fileHolder: Box[FileParamHolder] = Empty
          val bindForm =
            "type=file" #> SHtml.fileUpload((fph) => fileHolder = Full(fph)) &
            "type=submit" #> SHtml.ajaxSubmit("Submit", handleFile _)

         SHtml.ajaxForm(
           bindForm(form))
      }
}

You will get file element in fileHolder variable . In handleFile() , write your file functionality code .

About Ayush Mishra

Ayush is the Sr. Software Consultant @ Knoldus Software LLP. In his 5 years of experience he has become developer with proven experience in architecting and developing web applications. Ayush has a Masters in Computer Application from U.P. Technical University, Ayush is a strong-willed and self-motivated professional who takes deep care in adhering to quality norms within projects. He is capable of managing challenging projects with remarkable deadline sensitivity without compromising code quality. .
This entry was posted in Agile, AJAX, Java, LiftWeb, Scala, Web. Bookmark the permalink.

One Response to Scala LiftWeb: Upload File using AJAX

  1. rieshellin says:

    How is it different from others???

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s