1

how to send data using Postman in example DTO ,mainly Multipart file data,Angular 4,7

data in Multipartfile in Example DTO

public class ExampleDTo {

    private MultipartFile image;

    private String name;
    private String description;

}

Controller Mapping

@PostMapping()
public ResponseEntity<?> saveExample(@RequestParam("dtoAnduploadingFiles") ExampleDTo  dtoAnduploadingFiles  ) throws IOException {

}
UsamaAmjad
  • 4,175
  • 3
  • 28
  • 35
Myjaysan
  • 59
  • 1
  • 5
  • Hi Myjaysan in angular if uploading file its not work with same code you have to be handle few more things in angular if you know that good if you dont please reply me – harkesh kumar Jan 15 '19 at 04:55

2 Answers2

0

One way to do this is to use multiple multiparts.

So for example if you use this controller:

@PostMapping
public void uploadFileWithData(
        @RequestPart ExampleDTo request,
        @RequestPart("file") final MultipartFile file){
    ...
}

note: ExampleDto should contain only fields of json payload, not MultipartFile

In Postman you should use then form-data and choose file which you want to upload and also file with json payload. postman

bilak
  • 4,526
  • 3
  • 35
  • 75
0

You can do it like a normal multipart Form from Postman but you need to update your Mapping Method.

@PostMapping("/upload-file-form")
    public ResponseEntity<?> multiUploadFileModel(@ModelAttribute ExampleDTo model) {
        try {
            saveUploadedFile(model.getImage()); // Create method to save your file or just do it here
            formRepo.save(mode.getName(),model.getDescription()); //Save as per requirement 
        } catch (IOException e) {
            return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity("Successfully uploaded!", HttpStatus.OK);
    }

For complete code example look here. And then you can test it on postman like this: enter image description here

UsamaAmjad
  • 4,175
  • 3
  • 28
  • 35