简体   繁体   English

Ajax请求正确循环,但没有发送到服务器以获取更多的多个文件。 仅在阵列中保存一个文件

[英]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. 我有一个正在运行的ajax请求,因为我能够看到文件数量,文件名和循环。 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). 我有一个@RequestParam用于两个变量,一个数字(字符串)和一个文件(一个数组)。 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"); return(“文件上传”); This breaks out of the loop. 这打破了循环。 Needed to put return after the loop ends if you want it to run completely. 如果您希望循环完全运行,则需要在循环结束后放入return。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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