Springmvc uploading files using AJX does not set form Enctype

Source: Internet
Author: User
Tags save file

Recently in doing a small project encounter this problem solution as follows

     The 1.js code below gets the current form converted to FormData Ajax submission to the background var form = $ ("#importForm"); var formData =new FormData ($scope. _window.find ("# Importform ")); $.ajax ({                           type:" POST ",                         data:formdata,                         processdata:false,                         contenttype:false,                        URL: $scope. webpath.p0path+ "/cps-p0-web/importpiccontroller/importpickticket",                        success:function (data) {                           Alert ("Upload successful");                        },                        exception:function (data,textstatus) {                            }                    });

Background Code parsing

@ResponseBody @RequestMapping (value= "/importpickticket")     PublicResponsemodel Importpickticket (httpservletrequest request,httpservletresponse response)throwsbussinessexception{Responsemodel Responsemodel=NewResponsemodel (); Try{
Save file after uploading file String FilePath=fileupload.fileupload (request, response); responsemodel.setsuccess (true); }Catch(bussinessexception e) { }Catch(Exception e) {

}
return NULL; } return NULL; }

FileUpload Code

 Public classFileUpload { Public StaticString fileUpload (httpservletrequest request, httpservletresponse response) {Try {//File Server nameStringBuffer rename =NewStringBuffer ();//ServletContext ServletContext =request.getsession (). Getservletcontext ();//Get Server save pathString Pathurl = Servletcontext.getrealpath (Propertyplaceholder.getproperty ("Fileuploadpath"). toString ());//Parser Parses the context of the requestCommonsmultipartresolver Multipartresolver =NewCommonsmultipartresolver (ServletContext);//first, determine if the request includes data of type multipartif(Multipartresolver.ismultipart (Request)) {//then convert the data in request to the multipart type of dataMultiparthttpservletrequest multirequest =(multiparthttpservletrequest) request;//Get file nameIterator<string> iter =multirequest.getfilenames (); while(Iter.hasnext ()) {Multipartfile file=multirequest.getfile (String) Iter.next ());if(File! =NULL) {rename.append (NewRandom (). Nextint (99999)). Append ("_"). Append (System.currenttimemillis ()). Append (File.getoriginalfilename ()); File LocalFile=NewFile (Pathurl + "/" +rename.tostring ());//write files to localFile.transferto (LocalFile); }}}returnPathurl + "/" +rename.tostring ();} Catch(Exception e) {Throw NewBussinessexception ("FileUpload.fileUpload.BusinessException", E); }}}

Springmvc uploading files using AJX does not set form Enctype

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.