0

我正在尝试使用 ajax post 中的文件发送一些其他数据

   var fileInput = document.getElementById('file');
     var creditcardid =  document.getElementById('creditcardid');
     var file = fileInput.files[0];
   var formData = new FormData();

     formData.append('creditcardid', 'creditcardid');
     formData.append('file', file);

      $.ajax({
        url: url,
        data: formData,
        cache: false,
        contentType: false,
        processData: false,
        type: 'POST',
        success: function(response) {
            document.getElementById('statusMsg').innerHTML="<fmt:message key="fileUpload.success"/>"; 
            Success();
       }

In controller i am writing the code as

@RequestMapping(value = "/fileUpload", method = RequestMethod.POST)
public @ResponseBody String fileUpload(@RequestParam(value = "creditcardid")Long creditcardid,@RequestParam(value = "file") MultipartFile file,Model model, HttpServletRequest request) {

    String response = "generic.unexpectederror";
    try {
        model.addAttribute("message", "File '" + file.getOriginalFilename() + "' uploaded successfully");
        logger.info("Size of the file is " + file.getSize());
        logger.info("Name of the file is "  + file.getOriginalFilename());
        response = "fileUpload.success";

    } catch (DataIntegrityViolationException e) {
        response = "fileUpload.error";
        logger.error(ExceptionUtils.getStackTrace(e));

    }
    return response;
 }

但我的代码没有调用控制器。请让我知道如何接收数据和文件。谢谢

4

0 回答 0