简体   繁体   中英

Ajax request looping correctly but not sending to the server for more multiple files. Only saves one file in the array

I have an ajax request which is working as I am able to see the number of files, the file names and the looping. I am trying to get these saved to a local folder on my computer. I have an @RequestParam for two variables, a number (string) and files (an array). It seems to work for one file, but does not save the next file. Can anyone figure out why ?

    function makeProgress(number){   
      var url = getRelativeURL("web/fileUpload");        
      var formData = new FormData();
      formData.append('number', number);
      fls = document.getElementById("attachmentFileUploadInput").files; //number of files... 
      console.log(fls);
      for(j=0;j<fls.length;j++){
          formData.append('files[]', fls[j]);  //note files[] not files
          $.ajax({
              url : url,
              data : formData,
              processData : false,
              contentType: false,
              type : 'POST',
              success : function(data) {
               FileUploadVisible(true);
               $('#attachmentModal').modal('hide')
               $(':input','#attachmentModal').val("");
                $("#pbarmain").hide();
                $("#pbar").hide();
                $("#actionPlanDiv").hide();
                setObjectEnabled('#Upload',false);
              },
              error : function(err) {
                  FileUploadErrorVisible(true);
              }
         });
          console.log('loop each file working');
      }
      console.log("form data " + formData);


        }

Server side - this is where something is going wrong, only saving the first file in a folder:

private static String UPLOADED_FOLDER = "C://temp//";

@RequestMapping(value = { "/fileUpload" }, method = RequestMethod.POST)
@ResponseBody
public String uploadFile( @RequestParam("number") String number, @RequestParam("files[]") MultipartFile[] files, MultipartHttpServletRequest req, HttpServletResponse res)
{       

    for (MultipartFile file : files) {
    try {
        File directory = new File(UPLOADED_FOLDER + number);
                if (! directory.exists()){
                    directory.mkdir();
                  }
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + number + "//" + file.getOriginalFilename());
            Files.write(path, bytes);
            logger.info("You have successfully uploaded '" + file.getOriginalFilename() + "'");
            return("File Uploaded");


    } catch (Exception e) {
        res.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        logger.error("Failed to upload file '" + file.getOriginalFilename() + "'", e);
        return("File Not Uploaded");
    }
}
    return "redirect:/fileUpload";
}

}

return("File Uploaded"); This breaks out of the loop. Needed to put return after the loop ends if you want it to run completely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM