HTML5 Ajax File Upload progress bar How to display _ajax related

Source: Internet
Author: User
Tags file upload file upload progress bar

Originally intended to use the jquery plug-ins for asynchronous file upload, such as uploadfy but need additional support, but also some people with an IFRAME imitate asynchronous upload mechanism, feel more awkward. Because the project does not consider the lower version of the browser, it is decided to implement with HTML5. The following is just a simple demo, the specific style needs to do their own.
Background based on STRUT2 for file processing, depending on the project.  Just be aware of setting the file size limit. <constant name= "struts.multipart.maxSize" value= "52428800"/> This configuration is based on specific circumstances, more than this value will be reported 404.
The first is the upload page, relatively simple, with the file on this parameter.

upload.jsp

<% @page language= "java" pageencoding= "UTF-8" contenttype= "text/html"; 
Charset=utf-8 "%> <% String Path = Request.getcontextpath (); %> <! DOCTYPE html>  

Fd.append ("Name", document.getElementById (' name '). value);
Fd.append ("filename", document.getElementById (' filename '). Files[0]);
the two sentences are to bind the data to the form. Because HTML5 supports multiple file uploads,
document.getElementById (' FileName '). Files
the array is returned. There's only one file, so remove the element labeled 0.

Xhr.upload.addEventListener ("Progress", uploadprogress, false);

Xhr.addeventlistener ("Load", Uploadcomplete, false);

Xhr.addeventlistener ("Error", uploadfailed, false);

Xhr.addeventlistener ("Abort", uploadcanceled, false);
This binds progress, uploads, errors, interrupted events, and provides some interaction. The file progress display is displayed in the progress callback.
Then post the background code and the action configuration, Uploadifytestaction.java

Package com.bjhit.eranges.actions.test;

Import Java.io.File;

Import Com.opensymphony.xwork2.ActionSupport;

public class Uploadifytestaction extends Actionsupport {
 private static final long Serialversionuid = 837481714629791 752L;
 Private File fileName;
 private String name;
 Private String Responseinfo;

 Public String Doupload () throws Exception {
 System.out.println (name);
 File myFile = fileName;
 System.out.println (Myfile.getname ());
 Responseinfo = "Upload success!";
 return "Doupload";
 }

 Public String GetName () {return
 name;
 }

 public void SetName (String name) {
 this.name = name;
 }

 Public File GetFileName () {return
 fileName;
 }

 public void Setfilename (File fileName) {
 this.filename = fileName;
 }

 Public String Getresponseinfo () {return
 responseinfo;
 }

 public void Setresponseinfo (String responseinfo) {
 this.responseinfo = responseinfo;
 }
}

Action Configuration

<!--File Upload example-->
<action name= "uploadifytest_*" class= " Com.bjhit.eranges.actions.test.UploadifyTestAction "method=" {1} ">
 <result name=" doupload "type=" JSON " >
 <param name= "includeproperties" >responseInfo</param>
 <param name= " Excludenullproperties ">true</param>
 </result>
</action>

So the basic upload function is realized.

Thank you for your reading, I hope this article will help you learn Ajax way File Upload progress bar implementation method.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.