文件上传.NET Core'IFormFile'不包含'SaveAsASync'的定义,并且没有扩展方法 [英] File upload .NET Core 'IFormFile' does not contain a definition for 'SaveAsASync' and no extension method

查看:251
本文介绍了文件上传.NET Core'IFormFile'不包含'SaveAsASync'的定义,并且没有扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用ASP.NET Core Web Api上传文件. 我发现了这么多代码:

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();
    }
}

我得到错误IFormFile不包含SaveAsASync的定义,并且没有扩展方法. 有什么主意吗?

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

推荐答案

请参阅 https://github.com/aspnet/HttpAbstractions/issues/610 解释了为什么该方法被取代

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

这篇关于文件上传.NET Core'IFormFile'不包含'SaveAsASync'的定义,并且没有扩展方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆