How can I upload a file in MVC 6 under vNext?

asp.net-core, asp.net-core-mvc

Solution

FileUpload isn't implemented in MVC6 yet, see this issue, and related issues such as this one for status.

You can post an XMLHttpRequest from JavaScript and catch it with something like this code:

public async Task<IActionResult> UploadFile()
{
    Stream bodyStream = Context.Request.Body;

    using(FileStream fileStream = File.Create(string.Format(@"C:\{0}", fileName)))
    {

        await bodyStream.CopyToAsync(fileStream);

    }

  return new HttpStatusCodeResult(200);
}

Edit: If you see the issues linked, they have now been closed. You can use a more normal way to upload files in MVC6 now.

Problem

In MVC 5 I used to do this: ``` var context = (HttpContextBase)Request.Properties["MS_HttpContext"]; var file = (HttpPostedFileBase)context.Request.Files[0]; ``` Now these are not available in MVC 6 under vNext. How can I get the file(s) from the request?

Original source

Related problems