Monitored Ajax File Upload - For Struts 2

Upload a file to the server and asynchronously monitor its progress.

Have a go! Upload a file (all files are deleted after upload)

Features

Download v0.03

Instructions

  1. Add the jar files in the distribution to your to your WEB-INF/lib directory of your struts 2 application or alternatively use these maven2 dependencies (remember these are used alongside the struts 2 dependencies).
    <dependency>
    	<groupId>commons-fileupload</groupId>
    	<artifactId>commons-fileupload</artifactId>
    	<version>1.2</version>
    </dependency>
    <dependency>
    	<groupId>commons-io</groupId>
    	<artifactId>commons-io</artifactId>
    	<version>1.3.1</version>
    </dependency>
    <dependency>
    	<groupId>net.sf.json-lib</groupId>
    	<artifactId>json-lib</artifactId>
    	<version>2.0</version>
    	<classifier>jdk15</classifier>
    </dependency>
  2. Add the jsp tags to your page
    <%@ taglib uri="http://www.davidjc.com/taglibs"     prefix="djc"     %>
    <head>
    	...
    	<djc:head />
    	...
    </head>
    <body>
    	...
    	<djc:ajaxfileuploadform action="demo" dobefore="" doafter="" />
    	...
    </body>
  3. Configure your struts.xml file
    <package name="ajaxfileupload" namespace="/" extends="ajaxfileupload-default">
    	<action name="demo" class="com.davidjc.ajaxfileupload.action.Demo">
    		<interceptor-ref name="fileUploadStack" /> 
    		
    		<result name="success" type="httpheader">
    			<param name="status">200</param>
    		</result>
    	</action>
    </package>
  4. Extend com.davidjc.ajaxfileupload.action.FileUpload
    package com.davidjc.ajaxfileupload.action;
    
    import java.io.File;
    import org.apache.log4j.Logger;
    import com.opensymphony.xwork2.Action;
    
    public class Demo extends FileUpload{
    
        private final Logger logger = Logger.getLogger(Demo.class);
    
        public String execute() {
    	File uploadedFile = this.getUpload();
    	String contentType = this.getUploadContentType();
    	String fileName = this.getUploadFileName();
    	
    
    	return Action.SUCCESS;	
        }
        
    }

Alternative Configurations

There are numerous alternative ways to implement this using the tools ive provided. The above example is the simplest and quickest.

Feedback - Feature Request

Comments

Donate

This is probably not really worthy of donation as its such as small project, however..... if you find it useful and want to show your appreciation - i really love money!

Of course, this is not necessary, even if you plan on using this commercially.
Amount in £

Credits