Uploading a picture form when a form is submitted Ajax submission

Source: Internet
Author: User

Page

<script type= "Text/javascript" src= "js/jquery.form.js" ></script>
<script language= "JavaScript" >
$ (function () {

Submit Form
$ ("#add"). Click (function () {
if ($ (' #addForm '). Form (' Validate ')) {
var options = {
URL: ' ${currentbaseurl}/doadd ',
Type: ' Post ',
Success:function (data) {
if ("OK" ==data.message)
Window.location.href=data.backurl;
else{
$.messager.alert (' hint ', data.message);
$ ("#imageFile"). Val (""). Focus ()
}
}
};
$ (' #addForm '). Ajaxsubmit(options);
}
});
})
</script>

<form method= "POST" class= "ValidForm" id= "AddForm" name= "AddForm" enctype= "Multipart/form-data">
<input type= "File" id= "ImageFile"Name=" imagefile "class=" txt w240 easyui-validatebox "/>
<input type= "button" id= "Add" class= "btn" value= "Commit"/>
</form>

Controller class

@RequestMapping (value = "Doadd", method = {Requestmethod.post})
@ResponseBody
Public httpjsonresult<object> Doadd (multiparthttpservletrequestRequest, HttpServletResponse response, Fbnews News) {
httpjsonresult<object> Jsonresult = new httpjsonresult<object> ();
try {
multipartfile multipartfile = Request.getfile ("ImageFile");
if (null! = Multipartfile && multipartfile.getsize () > 0) {
Extended Name
String extend = Multipartfile.getoriginalfilename (). substring (Multipartfile.getoriginalfilename (). LastIndexOf (".") + 1). toLowerCase ();
if (!isimg (extend)) {
Jsonresult.setmessage ("Please upload pictures!");
return jsonresult;
}

File Tmpfile = new file (Buildimgpath (Request) + "/" + multipartfile.getoriginalfilename ());
if (!multipartfile.isempty ()) {
byte[] bytes = Multipartfile.getbytes ();
Bufferedoutputstream stream = new Bufferedoutputstream (new FileOutputStream (tmpfile));
Stream.Write (bytes);
Stream.Close ();
}
}
/** other code omitted **/

Jsonresult.setmessage ("OK");

Add successful return list page
Jsonresult.setbackurl (domainurlutil.getjm_url_resources () + "/admin/fbfc/news");
return jsonresult;
} catch (Exception e) {
throw new RuntimeException ();
}

}

Whether it is a picture

Private Boolean isimg (String extend) {
list<string> list = new arraylist<string> ();
List.add ("JPG");
List.add ("JPEG");
List.add ("BMP");
List.add ("GIF");
List.add ("PNG");
List.add ("TIF");
return list.contains (Extend);
}

Path to image upload

Private String Buildimgpath (HttpServletRequest request) {
String path = "Upload";
SimpleDateFormat formater = new SimpleDateFormat ("YyyyMMdd");
Path + = "/" + Formater.format (new Date ());
Path = Request.getrealpath (path);
File dir = new file (path);
if (!dir.exists ()) {
try {
Dir.mkdirs ();
} catch (Exception e) {
Log.error ("error", e);
}
}
return path;
}

Uploading a picture form when a form is submitted Ajax submission

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.