Use HTML+AJAX+FORMDATA+SPRINGMVC to implement a single file, and multiple file upload functions __html

Source: Internet
Author: User
Tags uuid
HTML code
<input type= "File" multiple= "true" id= "file" name= "file"/><button class= "U-button u-button-primary" onclick= "UploadFileMethod1 ()" id= "Doupload" > Confirm accessories </button>

JS Code

Single File Upload

function Uploadfilemethodsinglefile ()
  {
	 
      var filecontroller = $ctx + "/fankuicontroller/savefiles";                    The background address//FormData object that receives the uploaded file is
      var fileName = document.getElementById ("file"). Value;
     Alert (GetPath (fileobj));
      var form = new FormData ();

      var fileobj = document.getElementById ("file"). Files[0]; Gets the file object
       form.append ("file", fileobj);   File Object  
 
      var xhr = new XMLHttpRequest ();
      Xhr.open ("Post", Filecontroller, true);
      Xhr.send (form);
      Xhr.onload = function () {
          alert ("Upload complete!");
      }
  

When a single file is uploaded, Controller accepts the file with a multipartfile type of parameter
@RequestMapping (method = Requestmethod.post,value = "SaveFiles") public @ResponseBody map<string,object> SaveFiles (@RequestParam (value = "file") multipartfile file, HttpServletRequest request) {map<string,object> reg=  
		New Hashmap<string,object> ();
		
		Writes in-memory data to disk SYSTEM.OUT.PRINTLN ("Start attachment upload");
				try {//string FilePath = Request.getsession (). Getservletcontext (). Getrealpath ("/");
				String FilePath = "c:\\";
				Multipartfile file = Filearray[i];
				String originalfilename = File.getoriginalfilename ();
				String NewFileName = Uuid.randomuuid () +originalfilename;
				String Finalpath = filepath+newfilename;
				System.out.println (OriginalFilename);
				System.out.println (NewFileName);
				System.out.println (Finalpath);
				SYSTEM.OUT.PRINTLN ("parameter" +request.getparameter ("Json_filesnamearray"));
				
				System.out.println ("File" +file.getcontenttype ());
				File Fileattach = new file (Finalpath);
			
		File.transferto (Fileattach); catch (Exception E1) {
			E1.printstacktrace ();
		  After successful upload, add the attachment to the BLOB field in the Database Attachment table reg.put ("Result", "success");
	return reg; }

If multiple files are uploaded fomdata use Formdata.append () to iterate over multiple files and upload them

function UploadFileMethod1 () {var filecontroller = $ctx + "/fankuicontroller/savefiles";
     The background address//FormData object that receives the uploaded file is var fileName = document.getElementById ("file"). Value;
      Alert (GetPath (fileobj));
      var form = new FormData ();                        Form.append ("Json_filesnamearray", fileName);
Can add form data//var Filearray = new Array ();  for (var i=0;i<filearray.length;i++)//{//var fileobj = document.getElementById ("file"). Files[i];
Gets the file object//Filearray.push (fileobj);
      var files = document.getElementById ("file"). files;
    	  for (var i=0; i< files.length; i++) {alert (files[i].name);   Form.append ("Filearray", Files[i]);
      File Object}//XMLHttpRequest object//var fileobj = document.getElementById ("file"). Files[0];
      Form.append ("Filearray", fileobj);
      var xhr = new XMLHttpRequest ();
      Xhr.open ("Post", Filecontroller, True); Xhr.sEnd (form);
      Xhr.onload = function () {alert ("Upload complete!");
  }; }
Multi-File upload, the controller end, to receive when the use of multipartfile[] type parameters to accept the array type of multiple files, and then walk through the array to operate

@RequestMapping (method = Requestmethod.post,value = "SaveFiles") public @ResponseBody map<string,object> SaveFiles (@RequestParam (value = "Filearray") multipartfile[] Filearray, httpservletrequest request) {map<string,  
		Object> reg=new hashmap<string,object> ();
		Writes in-memory data to disk SYSTEM.OUT.PRINTLN ("Start attachment upload");
		System.out.println (filearray.length); try {for (int i=0;i<filearray.length;i++) {//string FilePath = Request.getsession (). Getservletcontext (). Getr
				Ealpath ("/");
				String FilePath = "c:\\";
				Multipartfile file = Filearray[i];
				String originalfilename = File.getoriginalfilename ();
				String NewFileName = Uuid.randomuuid () +originalfilename;
				String Finalpath = filepath+newfilename;
				System.out.println (OriginalFilename);
				System.out.println (NewFileName);
				System.out.println (Finalpath);
				SYSTEM.OUT.PRINTLN ("parameter" +request.getparameter ("Json_filesnamearray"));
				
				System.out.println ("File" +file.getcontenttype ()); File FIleattach = new File (Finalpath);
			File.transferto (Fileattach);
		} catch (Exception E1) {e1.printstacktrace (); }



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.