0

I tried and tried too different approaches but my error is still same, It can not be resolved from my side. its showing

Resolved exception caused by Handler execution: org.springframework.web.multipart.support.MissingServletRequestPartException: Required request part file is not present

My jsp code is

<form name="upload_document_form" onsubmit="return false" enctype="multipart/form-data">
<input type="file" name="file"/>
<button class="btn btn-primary btn-lg" name="upload_document_form_btn" id="upload_document_form_btn" onclick="UploadDocuments()">Upload</button>
</form>

Ajax call function

function UploadDocuments(){
    var formData = new FormData();
        formData.append('file',$("#file").val());  

    $.ajax({
        type: 'POST',
        url: 'http://localhost:8080/insertDocumentData',
        enctype: 'multipart/form-data',
        data: formData,
        type: 'POST',
        dataType:'json',
        contentType: false,
        processData: false,
        success: function(msg) {

        }
    });
}

Controller is

private static String UPLOAD_FOLDER = "uploaded_Doc/AP12345";

@PostMapping("/insertDocumentData")
public boolean insertDocumentData(@RequestParam("file") MultipartFile file,RedirectAttributes redirectAttributes) throws IOException{


    if (file.isEmpty()) {
        System.out.println("file is empty");
        return false;
    }

    try {
        byte[] bytes = file.getBytes();
        Path path = Paths.get(UPLOAD_FOLDER +"/"+   file.getOriginalFilename());
        Files.write(path, bytes);

    } catch (IOException e) {
        e.printStackTrace();
    }

    return  true;
 }
New-Learner
  • 229
  • 1
  • 3
  • 15
  • Possible duplicate of: https://stackoverflow.com/questions/43936372/upload-file-springboot-required-request-part-file-is-not-present – Sid Feb 11 '19 at 12:59
  • @Sid this not worked for me. I don't know where is my mistake but this same code is working for me on form submit without ajax – New-Learner Feb 11 '19 at 13:16

0 Answers0