0

Current request is not a multipart request Spring boot error while trying to upload a Image file.

Spring Boot

@PostMapping(value = "/add-item")
public ResponseEntity<?> handleProductInsert ( @RequestParam MultipartFile thumbnailFile ){
  try{
          .....................
          .....................
        return new ResponseEntity("Product added successfully", HttpStatus.OK);
    }catch (Exception e){
        return new ResponseEntity("Internal Server Error. Try again later", 
          HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

React App

state={data:null}

   handleChange=(e)=>{
     this.setState({data:e.target.files[0]});
   }

   connectToDatabase=async()=>{
        return await axios.post(`https://localhost:8080/add-item`, this.state.data);
    }

   render()=>{
       return (<>
                <input accept="image/*" onChange={this.handleChange} type="file" />
                <button onClick={this.connectToDatabase}>Submit</button>
               </>
              )
}

3 Answers3

0

try to change @PostMapping when MULTIPART_FORM_DATA_VALUE from import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;

@PostMapping(value = "/add-item", consumes = MULTIPART_FORM_DATA_VALUE)
public ResponseEntity<?> handleProductInsert ( @RequestParam MultipartFile thumbnailFile ){
  try{
          .....................
          .....................
        return new ResponseEntity("Product added successfully", HttpStatus.OK);
    }catch (Exception e){
        return new ResponseEntity("Internal Server Error. Try again later", 
          HttpStatus.INTERNAL_SERVER_ERROR);
    }
}
Dmitrii B
  • 2,672
  • 3
  • 5
  • 14
  • I DID THIS, IT SHOWS org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded' not supported – Dukenukem97 Jan 22 '21 at 14:31
0

form-data requires a key along with the file in Controller. Try:

@PostMapping(headers = { "Content-Type=multipart/form-data" }, value = "/add-item")
public ResponseEntity<?> handleProductInsert(@RequestParam("file") MultipartFile thumbnailFile){
  try{
          .....................
          .....................
        return new ResponseEntity("Product added successfully", HttpStatus.OK);
    }catch (Exception e){
        return new ResponseEntity("Internal Server Error. Try again later", 
          HttpStatus.INTERNAL_SERVER_ERROR);
    }
}

On Client Application, prepare request:

<form id='form' encType="multipart/form-data" method="post">
    <input id='import-field' type="file" name="file" accept=".jpg" onChange={onUpload} />
</form>
const onUpload = event => {
    if (event.target.files.length > 0) {
        let formElement = document.getElementById('import-field');
        uploadFile(new FormData(formElement)); //do POST call here
    }
};
0

It is needed to specify the content type header

axios.post('upload_file', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
})

Or use a shortcut method postForm, which is just the corresponding http method with the Content-Type header preset to multipart/form-data, see https://github.com/axios/axios#using-multipartform-data-format

axios.postForm('upload_file', formData)
banterCZ
  • 1,551
  • 1
  • 22
  • 37