0

I am trying to send multiple files along with some data for every file. This is my model:

public class FileDTO
{
    [Required]
    public IFormFile File { get; set; }
    [Required]
    public string CategoryName { get; set; }
    [Required]
    public string CategoryDescription { get; set; }
    public string Detail { get; set; }
}

This is my controller:

[HttpPost("Upload/{id:int}")]
public async Task<IActionResult> Upload(int id, IEnumerable<FileDTO> appFileDTOs)
{
    ...
}

Is this even a correct way to do so? How do I send such a request in Postman to simulate it?

Thanks in advance!

Edit

I tried it like this in Postman:

Postman Screenshot

Everything submits correctly besides the image. For some reason the image is always null...

1
  • When you it your controller, what is the value of 'var files = HttpContext.Request.Form.Files;' ? Commented Dec 2, 2019 at 9:15

2 Answers 2

1

[] represents collection/dictionary index while dot(.) represents there's a property.

So you should rename all the field names with the dot representation.

For example, change

appFileDTOs[0][File]

to

appFileDTOs[0].File

Demo

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot!! Would be good if there would be a better GUI from Postman for that kind of scenario. The way you have to send the data seems a little bit like a hack to me.
0

try this it may help you,

send from formData.

in model key send value as

[
{
"CategoryName":"Category1",
"CategoryDescription ":"Category1 Description",
"Detail":"Details "
},
{
"CategoryName":"Category2",
"CategoryDescription ":"Category2 Description",
"Detail":"Details2"
}

]

and for file send first file as file1 and second file as file2; enter image description here

In server side , remove IEnumerable of FileDTO appFileDTOs from method name. get value of model as

var data = JsonConvert.DeserializeObject<List<FileDTO>>(Request.Form["model"]);

simillary for file

   var fileUpload1 = Request.Form.Files["file1"];
   var fileUpload2 = Request.Form.Files["file2"];

1 Comment

Thanks, I will try it later!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.