12

I'm working on a dotnet core WebAPI 2.1 and I can't find a way of sending to into the Body an image.

The controller looks like this:

    [HttpPost("api/image")]
    public IActionResult Post([FromBody]IFormFile file)
    {
        var filePath = Path.GetTempFileName();
        if (file.Length > 0)
        {
            return Ok();
        }
        return BadRequest();
    }

And this is my Postman call: enter image description here

This call is never finishing as the Kestrel is failing enter image description here

I've already tried to use Consumes

[Consumes("application/json", "application/json-patch+json", "multipart/from-data")]

Also in postman, I have set Content-Type to multipart/form-data

rm -rf .
  • 473
  • 1
  • 4
  • 14

5 Answers5

16

I found the solution for .Net Core 2.1/2.2 here

POST multiple files from POSTMAN

POST single file from POSTMAN

    [HttpPost]
    public async Task<IActionResult> UploadSingleFile([FromForm(Name = "file")] IFormFile file)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);
        var IDsList = new IDsList();

        try
        {
            var id = SaveFile(file);
            IDsList.Files.Add(new IDsList.FileInfo() { id = id, fileName = file.FileName });
        }
        catch (Exception ex)
        {
            return BadRequest(ex.Message);
        }

        return Content(JsonConvert.SerializeObject(IDsList), "application/json");
    }

    [HttpPost("UploadFiles")]
    public async Task<IActionResult> UploadFiles([FromForm(Name = "files")] ICollection<IFormFile> files)
    {
        if (!ModelState.IsValid)
            return BadRequest(ModelState);

        //var files = Request.Form.Files?.GetFiles("files");
        String message = String.Empty;
        int filesCounter = 0;
        var IDsList = new IDsList();

        foreach (var file in files)
        {
            if (file.Length == 0)
                message = message + $"errorDescription {file.FileName}\n";

            try
            {
                var id = SaveFile(file);
                IDsList.Files.Add(new IDsList.FileInfo() { id = id, fileName = file.FileName });
                filesCounter++;
            }
            catch(Exception ex)
            {
                message = $"{message}{ex.Message}\n";
            }
        }

        IDsList.message = $"Amount: {filesCounter}.\n{message}";

        return Content(JsonConvert.SerializeObject(IDsList), "application/json");
    }
6

Try doing it like this. Use the same request in postman you are using now. This is just crude boilerplate method but you get the idea.

Also, dont forget to set headers of your request in postman to: Content-Type: multipart/form-data

[HttpPost]
[Route("api/image")]
public async Task<IHttpActionResult> InsertNewMerchant()
{
        // your form data is here
             var formData = HttpContext.Current.Request.Form;
             HttpFileCollection files = HttpContext.Current.Request.Files;


            if (files.Count == 1)
            {
            // this is the file you need 
                var image = files[0];
                    // do something with the file
            }
    return StatusCode(System.Net.HttpStatusCode.Created);
}
Bola
  • 718
  • 1
  • 6
  • 20
  • 2
    Thanks for your answer. In dotnet-core `HttpContext` have been removed. Anyway, your solution it does work if we are using it like this `var files = Request.Form.Files;` – rm -rf . Jul 19 '18 at 14:45
5

Seems like there is an error problem with Postman macOS application. When I'm using the postman chrome extension everything works as expected.

MacOS app MacOS app

Chrome extension enter image description here

rm -rf .
  • 473
  • 1
  • 4
  • 14
3

I tried and it worked. Maybe you forget to do something. Remove [FromBody] attribute.

    [HttpPost("api/image")]
    public IActionResult Post(IFormFile file)
    {
        var filePath = Path.GetTempFileName();
        if (file.Length > 0)
        {
            return Ok();
        }
        return BadRequest();
    }

Postman automatically attaches the correct Content-Type, select form-data option in body section and add your file with file key.

It should work.

Celal Yildirim
  • 601
  • 1
  • 6
  • 13
  • I've tried that and for some reason, it doesn't work. @Bola's solution seems to be a good workaround. – rm -rf . Jul 20 '18 at 08:15
0

I know this has been answered, but you seem to having decided for an workaround. There is nothing wrong with your endpoint nor with the Postman request, I had the same issue and installing the native Postman for my OS solved the problem https://www.getpostman.com/apps

mariovalens
  • 365
  • 1
  • 13