#
#
#2019-04-28 13:25:02
.NET
#, 2019-04-28 13:25:02

Uploading a file in .Net Core 2 WebApi (from Postman), who has any successful experience?

there is no experience on all points of the subject ((the note https://janaks.com.np/file-upload-asp-net-core-web-api/
was taken as a basis, I try to load from Postman (yes, in the web frontend it’s generally zero, which yes that is ((
variants of the tried

controller attributes:
//[Consumes("multipart/from-data", "application/octet-stream")]
    [Consumes("application/json", "application/json-patch+json", "multipart/from-data", "application/x-www-form-urlencoded")]
    //[Consumes("application/json", "multipart/from-data")]
    //[Consumes("multipart/from-data")]
    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController]
method header options:
//public async Task<IActionResult> PostAttachmentItem([FromBody] IFormFile file)
        //public async Task<IActionResult> PostAttachmentItem(IFormFile file)
        public async Task<IActionResult> PostAttachmentItem(List<IFormFile> files)
request in Postman
OBR1TCi.pngmwWM6mq.png

if attributes and/or are used, we don't even get into the method body ((( 415 Unsupported Media Type immediately . If we remove all attributes, we get into the method, but the input parameter (it doesn't matter or ) is always equal to ((( help plz! what am I doing it wrong? (even if everything - where is the enlightenment breaking?! ))) ps https://docs.microsoft.com/ru-ru/aspnet/core/mvc/m... read until it helped ( (( pps AspNetCore.Docs uploaded, but there are examples based on core 1.1 (and the example is not going to be (((, but you need 2. * ((( [Consumes("..")][FromBody]IFormFile fileList<IFormFile> filesnull

Answer the question

In order to leave comments, you need to log in

1 answer(s)
#
#, 2019-04-28
@mindtester

the question is closed. Request.Form.Filesfound here https://qa-help.ru/questions/aspnet-core-rc2-zagru...
ps

code at the moment (everything works)
[HttpPost]
        public async Task<IActionResult> PostAttachmentItem()
        {
            var files = Request.Form.Files;
            long size = files.Sum(f => f.Length);
            foreach (var file in files)
                if (file.Length > 0)
                    using (var stream = new MemoryStream())
                    {
                        await file.CopyToAsync(stream);
                        db.AttachmentItems.Add(new AttachmentItem
                        {
                            FileName = file.FileName,
                            Body = stream.ToArray()
                        });
                    }
            await db.SaveChangesAsync();
            return Ok(new { count = files.Count, size });
        }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question