Monitored Ajax File Upload - For Struts 2
Upload a file to the server and asynchronously monitor its progress.Features
-
The server update checks are monitored continuously and will increase or decrease in frequency depending on the upload
speed. So for large files it will slow down progress update checks to stop server being flooded with connections.
Tested on Firefox 2, Opera 9.1, IE 7, IE 6. Please let me know of other browsers it works on (or doesnt work on!)
Provide a before and after javascript functions to call if you want.
Download v0.03
-
v0.03 - 24 October 2007
(jar file with dependencies - doesnt include struts2 jars)
Previous Versions
Instructions
-
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>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>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>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
-
I find if i upload a very large file and press stop, to stop the request, my browser can crash. However, if
i do an identical test without the ajax file upload script - the browser still crashes! So i guess its a browser issue and
not a bug in my script!
Remeber to have cookies enabled or the progress bar wont work (this bugged me for a while when testing in opera - as id
disabled cookies in that browser)
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.