Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReadAsMultipartAsync equvialent in .NET core 2

I'am rewriting a .net 4.5 application to aspnet core 2.0 and I have a method that i have some problem updating:

    [HttpPut]
    [Route("api/files/{id}")]
    public async Task<Person> Put(int id)
    {
        var filesReadToProvider = await Request.Content.ReadAsMultipartAsync();
        var fileStream = await filesReadToProvider.Contents[0].ReadAsStreamAsync();
        return _personService.UpdatePerson(id, fileStream);
    }

It seems that request no longer has Content, but body. Which is fine. But how how do i read the body if it is a MimeMultipart now?

I have looked into IFormFile, but don't I have to change something in the frontend?

Anything that will help me in the right direction is appreciated :)

like image 574
Moddaman Avatar asked May 16 '18 10:05

Moddaman


People also ask

Is .NET Core 2.2 end of life?

. NET Core 2.2 was released on December 4, 2018. As a non-LTS (“Current”) release, it is supported for three months after the next release. .

Does net5 replace NET Core?

Net 5 that is Opensource and Cross-platform, which will replace . Net Framework, . Net Core and Xamarin with a single unified platform called . Net 5 Framework.

What is the difference between .NET Core and Mono?

NET is platform dependent, Mono allows developers to build Linux and cross- platform applications. Mono's . NET implementation is based on the ECMA standards for C#. This paper examines both of these programming environments with the goal of evaluating the performance characteristics of each.

What is NET Core 3.1 used for?

. NET Core 3.1 is also known for Blazor, the ASP.NET Core technology that puts WebAssembly to use as a browser compilation target for higher-order programming languages so they can be used in Web development instead of JavaScript.


1 Answers

See File uploads in ASP.NET Core ... mainly the section of Uploading large files with streaming. It has to be something like

var boundary = MultipartRequestHelper.GetBoundary(
    MediaTypeHeaderValue.Parse(Request.ContentType),
    _defaultFormOptions.MultipartBoundaryLengthLimit);
var reader = new MultipartReader(boundary, HttpContext.Request.Body);

var section = await reader.ReadNextSectionAsync();
like image 142
Rahul Avatar answered Sep 21 '22 09:09

Rahul