Search code examples
c#asp.netasp.net-web-apifile-upload

File upload .NET Core 'IFormFile' does not contain a definition for 'SaveAsASync' and no extension method


I'm trying to upload a file using ASP.NET Core Web Api. As many i found this code:

namespace ModelBindingWebSite.Controllers
{
  public class HomeController : Controller
  {
    private IHostingEnvironment _environment;

    public HomeController(IHostingEnvironment environment)
    {
        _environment = environment;
    }
    public IActionResult Index()
    {
        return View();
    }

    [HttpPost]
    public async Task<IActionResult> Index(ICollection<IFormFile> files)
    {
        var uploads = Path.Combine(_environment.WebRootPath, "uploads");
        foreach (var file in files)
        {
            if (file.Length > 0)
            {
                var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                await file.SaveAsAsync(Path.Combine(uploads, fileName));
            }
        }
        return View();
    }
}

I get the error IFormFile does not contain a definition for SaveAsASync and no extension method. Any idea?


Solution

  • Please see https://github.com/aspnet/HttpAbstractions/issues/610 which explains why the method has been superceded