2

I'm trying to send a file from a postman to a web api, method on web api is expecting a list of type which contains doc type, file and folder name.. posted below:

Web api method:

[HttpPost("post-images")]
public async Task<IList<RepositoryItem>> PostAnImages (IList<PostRepoRequest> request)
{
    // rest of the code 
}

PostRepoRequest class:

public class PostRepoRequest
{
    public FileType FileType { get; set; }
    public IFormFile File { get; set; }
    public string Folder { get; set; }
}

As it's possible to notice I've never received a file, its allways null, I've tried also setting a header content-type as multipart/form-data but that didn't worked aswell..

What might be the trick here?

Thanks

Cheers

8
  • You should use multipart/form-data but accept and process it differently on the server side. You can check this link: learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/… Commented Mar 31, 2020 at 18:33
  • @OguzOzgul I've tried adding content-type as I said in post but that didn't helped I've received File as null... maybe because it's part of the list ?.. hmmm not sure Commented Mar 31, 2020 at 18:36
  • I think you need to use [FromForm]IFormFile (attribute) as parameter in your backend method Commented Mar 31, 2020 at 18:39
  • Did you check the link I've provided? Changing the content-type only is not enough. multipart/form-data has a format which uses boundaries to separate the parts. Please read the documentation. It must be handled differently. Commented Mar 31, 2020 at 18:40
  • You just need to take in a List<IFormFile> because all the other info is there already. Commented Mar 31, 2020 at 20:19

3 Answers 3

6

You need to change the request body with dot pattern like this:

enter image description here

Then you need to add [FromForm] attribute to the controller input parameter. Also note that the variable names in the postman and controller must match.

[HttpPost("post-images")]
public async Task<IList<RepositoryItem>> PostAnImages ([FromForm]IList<PostRepoRequest> repositoryItems)

With these changes, you will be able to get the request correctly:

enter image description here

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

8 Comments

thanks for example I will check it now ! What was wrong with me implementation where I used to receive List of PostRepoRequestDTO ? instead of only one PostRepoRequestDTO ?
this example really works but how come? Why did you convert List<PostRepoRequest> request to PostRepoRequestDTO request.. what was wrong with List<PostRepoRequest> request ? Wouldnt be nicer if we received few objects with those properties? instead of this arrays? Thanks a lot for your help
@Roxy'Pro you are right, no need to change request object, I updated the answer with a shorter a nicer way. Can you try please? All you need to change in code is adding [FromForm] as I indicated in the updated answer.
now when I've changed it to [FromForm] List<PostRepoRequest> request , now I can't receive data from postman :( Could you try also please?
@Roxy'Pro the input name on the controller and postman must match. Both of them must be same. Please try like this ([FromForm]IList<PostRepoRequest> repositoryItems), I updated it in the answer now.
|
1

Try to send file as separated parameter

[HttpPost("post-images")]
public async Task<IList<RepositoryItem>> PostAnImages (IList<PostRepoRequest> request, [FromForm]List<IFormFile> files)
{
    // rest of the code 
}

and in client (assuming that can be Angular):

let input = new FormData();
for (var i = 0; i < this.filesToUpload.length; i++) {
   input.append("files", this.filesToUpload[i]);
}

1 Comment

Good point but I need it as part of a list because I would like to send list of the files to web api...
0

There is an invisible selectbox, just move your cursor to left and you'll see the selectable area. Then select it as a file.

enter image description here

And add [FromBody] at he beginnig of ypur method parameter like ([FromBody]IList<PostRepoRequest> request)

And last, update the Key to ...[0][file] (you've forgot [])

2 Comments

it's allready a file, how otherwise I could select a file from computer :)
thanks for noticing that, I've tried that earlier also

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.