AXIOS implementation simple file upload function

Example of this article shared AXIOS to implement the specific code of the file upload function for everyone, for your reference, the specific content is as follows

front page:

introduced an AXIOS JS file

     HTML: 

   JS: 
Function UPLOAD () {Alert (“Upload”) Let File = Document.GtelementSbyName (‘IMG’) [0] .files [0]; Let Formdata = New Formdata (); FormData.Append (“UploadFile”, File. Name); const config = {headers: {“content-type”: “multipart / form-data; boundary =” + new date (). gettime ()}}; AXIOS .post (“/ file / upload”, FormData , config .Then (Function);};}) .catch (error) {console.log (error);});}

Backstage Receive:
  @POSTMAPPING (Value = "/ upload") public String upload (HttpServletRequest request) {logger.info ( "Start uploading ......"); MultipartHttpServletRequest servletRequest = (MultipartHttpServletRequest) request; Map  fileMap = servletRequest.getFileMap (); for (Map .Entry entry: fileMap.entrySet ()) {logger.info ( "name: {}", entry.getKey ()); MultipartFile multipartFile = (MultipartFile) entry.getValue (); try {FileUtil.writeFile (multipartFile.getInputStream (), PATH + "/ 12 _" + MultipartFile.getRiginalFileName ());} catch (ooexception e) {E.PrintStackTrace ();}} returnTRACE ();}} Return "{'RESULT': 'OK'}";} @RequestMApping (Value = "/ uploadII") public String uploadII (@RequestParam ( "uploadFile") MultipartFile uploadFile) {logger.info ( "upload: {}", uploadFile); try {inputStream inputStream = uploadFile.getInputStream (); String originalFilename = uploadFile.getOriginalFilename (); logger.info ( "file: {}", originalFilename); FileUtil.writeFile (inputStream, path + "/" + originalFilename);} catch (IOException e) {e.printStackTrace (); } Return "{'RESULT': 'OK'}";} 
POX.XML:

  Commons-FileUpload   

Injecting CommonSmultipartResolver :
    @Beanpublic CommonsMultipartResolver initCommonsMultipartResolver () {CommonsMultipartResolver resolver = new CommonsMultipartResolver (); resolver.setMaxUploadSize (104857600); resolver.setMaxInMemorySize (4096); return resolver;}    GitHub: AXIOS   The above is the full content of this article, I hope that everyone's learning is helpful, I hope everyone willHold Tumi Cloud.                      
© Copyright Notice
THE END
Just support it if you like
like0
share
comment Grab the couch

Please log in to comment