【问题标题】:Multiple Ajax Form Data Upload Jquery多个Ajax表单数据上传Jquery
【发布时间】:2017-09-09 15:02:10
【问题描述】:

我正在尝试将多个文件上传到一个文件夹,但由于我在日志中收到错误消息,这无法正常工作:“所需的 MultipartFile 参数‘文件’不存在”。请查看我的以下代码并指导我哪里出错了。

<input type="file" id = 'attachmentFileUploadInput' multiple style="display: none;" >

    function makeProgress(number){   
      var url = getRelativeURL("web/fileUpload");        
      var formData = new FormData();
      formData.append('number', number);
      fls = document.getElementById("attachmentFileUploadInput").files; //length of files... 
      for(j=0;j<fls.length;j++){
          formData.append('files[]', fls[j]);  //note files[] not files
      }
      // this wroked for single file upload -formData.append('file', $('input[type=file]')[0].files[0]); 
      console.log("form data " + formData);
      $.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);
          }
     });

        }

服务器端:

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

@RequestMapping(value = { "/fileUpload" }, method = RequestMethod.POST)
@ResponseBody
public String uploadFile( @RequestParam("number") String number, @RequestParam("file") MultipartFile file, MultipartHttpServletRequest req, HttpServletResponse res)
{       
    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");
    }
}

}

【问题讨论】:

  • 您将文件名附加到files[],但在服务器端期望名称为file 例如:@RequestParam("file") MultipartFile file
  • 我如何更新服务器端以期望文件[],我需要在我的代码上更新什么?
  • 您找到解决方案了吗?

标签: javascript java ajax jsp file-upload


【解决方案1】:

contentType: 'multipart/form-data'这就是你所缺少的

【讨论】:

  • 错误:原因:org.apache.commons.fileupload.FileUploadException:请求被拒绝,因为没有找到多部分边界
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多