0

fileUpload.html

    <p-fileUpload name="file" customUpload="true" (uploadHandler)="myUploader($event)"
 type="file" accept=".lsx, .xlsx" [auto]="true" ></p-fileUpload>

fileUpload.ts

    myUploader(event):void{

        const formdata:FormData = new FormData();
        const fileToUpload: File = event.files[0];

        formdata.append('file', fileToUpload);

        const req = new HttpRequest('POST', 'http://localhost:8081/fileUpload', formdata);

       this.httpClient.request(req).subscribe(); 
}

fileUpload.java

   @PostMapping(value = "/fileUpload")
    public ApiResponse giftCardBatchList(@RequestParam("file") MultipartFile file) {
        fileUploadService.upload(file);
        return new ApiResponse(ApiResponseStatus.SUCCESS, null);
    }

controller doesn't catch the request and I got this exception

MissingServletRequestPartException: Required request part 'file' is not present

if I change

@RequestParam("file") MultipartFile file

to

@RequestParam("file") MultipartFile[] file

controller catches the request but the file array contains nothing. It is empty :(

any ideas?

thank you guys !

4
  • Try this way @RequestParam MultipartFile file and let me know. Commented Dec 31, 2019 at 11:53
  • got same exception "Required request part 'file' is not present" :( Commented Dec 31, 2019 at 12:01
  • Try to pass content type in headers with request, like this .new HttpRequest('POST', 'http://localhost:8081/fileUpload', formData, {headers: headers}); refer this one stackoverflow.com/questions/48279484/… Commented Dec 31, 2019 at 12:26
  • I did exactly the same request with same headers but got the same exception :( Commented Jan 2, 2020 at 7:44

2 Answers 2

0

Try modifying your annotation as like below,

@PostMapping(value = "/fileUpload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public ApiResponse giftCardBatchList(@RequestParam(value = "file") MultipartFile file) {
        fileUploadService.upload(file);
        return new ApiResponse(ApiResponseStatus.SUCCESS, null);
    }
Sign up to request clarification or add additional context in comments.

4 Comments

didn't work :( get same exception Required request part 'file' is not present
can you check your request payload whether file is getting submitted ?
add this in your application.properties, # Enable multipart uploads spring.servlet.multipart.enabled=true # Threshold after which files are written to disk. spring.servlet.multipart.file-size-threshold=2KB # Max file size. spring.servlet.multipart.max-file-size=200MB # Max Request Size spring.servlet.multipart.max-request-size=215MB
didnt help these properties :(
0

Try modfying in ts file as below before calling post api

const formdata: FormData = new FormData(); const headers = new HttpHeaders().set('Content-Type','application/json');

formdata.append("file",fil[0]);

or formdata.set("file",fil[0]);

1 Comment

after these changes I got same exception :(

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.