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 (); }