An example of a simple jquery plug-in ajaxfileupload that uploads files through Ajax

Source: Internet
Author: User

Page code:

<HTML>
<! -- Introduce relevant JS files and relative paths -->
<SCRIPT type = "text/JavaScript" src = "JS/jquery. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "JS/Ajaxfileupload. Js "> </SCRIPT>

<! -- Execute the function for uploading files -->
<SCRIPT type = "text/JavaScript">
FunctionAjaxfileupload(){
$.Ajaxfileupload(
{
URL: 'Update. do? Method = Uploader ', // link to the server address
Secureuri: false,
Fileelementid: 'housemaps ', // ID attribute of the file selection box
Datatype: 'xml', // format returned by the server, which can be JSON
Success: function (data, status) // equivalent to the usage of the try statement block in Java
{
Certificate ('{result'{.html ('added successfully ');
},
Error: function (data, status, e) // equivalent to the Catch Block usage in Java
{
Certificate ('{result'{.html ('add failed ');
}
}

);

}
</SCRIPT>
</Head>

<Body>
<Form method = "Post" Action = "Update. do? Method = Uploader "enctype =" multipart/form-Data ">
<Input type = "file" id = "housemaps" name = "housemaps"/>
<Input type = "button" value = "Submit" onclick ="Ajaxfileupload() "/>
</Form>
<Div id = "result"> </div>

</Body>
</Html>

Server code:

Public class updateaction extends dispatchaction {

Public actionforward Uploader (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response ){
Upformform = (upformform) form;
Formfile FF = upformform. gethousemaps ();
Try {
Inputstream is = ff. getinputstream ();
File file = new file ("D:/" + ff. getfilename (); // specify the file storage path and file name
Outputstream OS = new fileoutputstream (File );

Byte [] B = new byte [1024];
Int Len = 0;
While (LEN = is. Read (B ))! =-1 ){
OS. Write (B, 0, Len );
}
OS. Close ();
Is. Close ();
} Catch (exception e ){
E. printstacktrace ();

}

Return NULL;
}

}

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.