1
votes

I am trying to upload several files to a web api endpoint with .net core 2.1 web api

this is my controller and the method to upload

[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
public class PagosController : ControllerBase{
        [HttpPost("UploadDescuento")]
        public async Task<IActionResult> UploadDescuento(IEnumerable<IFormFile> files)
        {

            return Ok();
        }
}

with postman i made a request to de controller but i am getting a badrequest the first time I thought my request is bad so i do the same request in postman

POST /api/pagos/UploadDescuento HTTP/1.1
Host: localhost:59039
User-Agent: PostmanRuntime/7.16.3
Accept: */*
Cache-Control: no-cache
Postman-Token: 7535f283-89fb-4f86-bbb9-3d78ec302ebe,19a709dd-37c9-48ef-a157-c21f99861260
Host: localhost:59039
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Accept-Encoding: gzip, deflate
Content-Length: 1320022
Connection: keep-alive
cache-control: no-cache


Content-Disposition: form-data; name="files"; filename="/C:/Users/jcpc9/Pictures/0-5616x3744.jpg


------WebKitFormBoundary7MA4YWxkTrZu0gW--,
Content-Disposition: form-data; name="files"; filename="/C:/Users/jcpc9/Pictures/0-5616x3744.jpg


------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="files"; filename="/C:/Users/jcpc9/Pictures/101-2621x1747.jpg


------WebKitFormBoundary7MA4YWxkTrZu0gW--
Content-Disposition: form-data; name="pago"

234235834
------WebKitFormBoundary7MA4YWxkTrZu0gW--

but im getting badrequest

{
    "": [
        "The input was not valid."
    ]
}
3

3 Answers

1
votes

my solution was remove de attribute ApiController i dont know why the documentation doesnt say to much [https://docs.microsoft.com/en-us/aspnet/core/web-api/?view=aspnetcore-2.2#multipartform-data-request-inference] i dont understend what is the problem

[Produces("application/json")]
    [Route("api/[controller]")]
    //[ApiController]<<remove this
    public class PagosController : ControllerBase

and the method

[HttpPost("UploadDescuento")]
public async Task<IActionResult> UploadDescuento(IEnumerable<IFormFile> files)

if someone know why this propertie "[ApiController]" causing this problema i hope you can explain or at leas an good article

0
votes

You're not uploading the file. You're just sending the path of the file on the client machine, which the server can't do anything with (unless the server and client machines will always be the same).

Postman does have the option of sending a file properly. See this answer.

0
votes

You need to select form-data in the body in postman and set up a key "files" as file type example