Jquery-ajaxfileupload asynchronous upload plug-in (with Engineering Code) _ jquery

Source: Internet
Author: User
Asynchronous file upload is required when processing file uploads. The JqueryAjaxFileUploader component is used here. The server uses struts2 to process file uploads. I download the project code.
Due to project requirements, asynchronous file upload is required when processing file uploads. Here we use Jquery Ajax File Uploader this component: http://www.phpletter.com/download_project_version.php? Version_id = 6
The server uses struts2 to process file uploads.
Required environment:
Jquery. js
Ajaxfileupload. js
Jar package on which struts2 depends
And struts2-json-plugin-2.1.8.1.jar
Compile the Action for File Upload

The Code is as follows:


Package com. ajaxfile. action;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. FileOutputStream;
Import org. apache. struts2.ServletActionContext;
Import com. opensymphony. xwork2.ActionSupport;
@ SuppressWarnings ("serial ")
Public class FileAction extends ActionSupport {
Private File file;
Private String fileFileName;
Private String fileFileContentType;
Private String message = "you have successfully uploaded the file ";
Public String getMessage (){
Return message;
}
Public void setMessage (String message ){
This. message = message;
}
Public File getFile (){
Return file;
}
Public void setFile (File file ){
This. file = file;
}
Public String getFileFileName (){
Return fileFileName;
}
Public void setFileFileName (String fileFileName ){
This. fileFileName = fileFileName;
}
Public String getFileFileContentType (){
Return fileFileContentType;
}
Public void setFileFileContentType (String fileFileContentType ){
This. fileFileContentType = fileFileContentType;
}
@ SuppressWarnings ("deprecation ")
@ Override
Public String execute () throws Exception {
String path = ServletActionContext. getRequest (). getRealPath ("/upload ");
Try {
File f = this. getFile ();
If (this. getFileFileName (). endsWith (". exe ")){
Message = "sorry, the format of the file you uploaded is not allowed !!! ";
Return ERROR;
}
FileInputStream inputStream = new FileInputStream (f );
FileOutputStream outputStream = new FileOutputStream (path + "/" + this. getFileFileName ());
Byte [] buf = new byte [1, 1024];
Int length = 0;
While (length = inputStream. read (buf ))! =-1 ){
OutputStream. write (buf, 0, length );
}
InputStream. close ();
OutputStream. flush ();
} Catch (Exception e ){
E. printStackTrace ();
Message = "sorry, File Upload Failed !!!! ";
}
Return SUCCESS;
}
}


Struts. xml

The Code is as follows:









Text/html




Text/html






Observe the result configuration in struts. xml in combination with Action.
The contentType parameter is required. Otherwise, the browser always prompts to save the returned JSON result as a file and will not submit it to ajaxfileupload for processing. This is because the default contentType of struts2 JSON Plugin is application/json, while the ajaxfileupload requires text/html.
Jsp page for File Upload

The Code is as follows:


<% @ Page language = "java" contentType = "text/html; charset = UTF-8"
PageEncoding = "UTF-8" %>




Insert title here

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.