我在VS 2015中使用的是ASP.NET5,MVC6.我在做一个网络应用程序.我的表格上有:

    <form method="post" enctype="multipart/form-data">
    <div id="uploadSection" >
        <label >Select files for upload</label>
        <input type="file" id="fileSelect" name="fileSelect" multiple />
    </div>

    <div>
        <input type="submit" id="thisbutton" value="button"  />
    </div>
    </form>

在我的控制器中:

        [HttpPost]
    public async Task<IActionResult> FileForm(FileViewModel vm, IFormFile file)
    {
        if (ModelState.IsValid)
        {
            //IFormFileCollection files = Request.Form.Files;
            string filePath = Path.Combine("C:", "transfers");
            //foreach (var file in files)
            //{
                if (file != null && file.Length > 0)
                {
                    var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
                    await file.SaveAsAsync(Path.Combine(filePath, fileName));
                }
            //}
            ModelState.Clear();
        }
        return View();
    }

我有一个更大的表单,表单中有母表单字段,这些字段将进入我的FormViewModel(所有字段都会按它们应该的方式填充),我实际上try 将IFormFileCollection、ICollection、IList、List作为上述方法中的参数,并将其直接放入我的视图模型中.无论是作为单个文件还是多个文件,在模型或控制器中,我都会得到null.

更棒的是,我用fiddler运行了多次,它实际上是在发布文件——那么为什么它们没有被转换到模型中呢?

我的依赖项有错吗?有人能想到这是什么原因吗?

  "dependencies": {
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.AspNet.Http":  "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.FileProviderExtensions" : "1.0.0-rc1-final",
    "Microsoft.Extensions.Configuration.Json": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Console": "1.0.0-rc1-final",
    "Microsoft.Extensions.Logging.Debug": "1.0.0-rc1-final",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final"
  },

推荐答案

您的文件输入具有"fileSelect"的名称属性.操作的参数名称是"file".让它们相匹配,这应该会奏效.

Asp.net相关问答推荐

502 DotNet WebApplication的网关nginx已损坏

$(document).ready 不工作

asp.net 单选按钮分组

在 appSettings 中存储字符串数组?

Automapper - 映射器已初始化错误

将命令行参数传递给 ASP.NET Core 中的 Startup 类

如何在asp.net中判断会话是否过期

无法加载文件或程序集'System.Web.WebPages.Razor,版本 = 3.0.0.0

显示单选按钮列表内联

Asp.net 中的下拉列表验证使用必填字段验证器

如何获得 System.Diagnostics.Process 的输出?

什么时候应该使用 Response.Redirect(url, true)?

无法在 IIS 中启动网站 - W3SVC 正在运行

在 ASP.NET 中使用 MasterPages 时使用 JQuery 的正确方法?

如何使用restsharp下载文件

ASP.NET 发布try 复制不存在的文件

HttpContext.Current属性的跨线程使用及相关的东西

elmah:没有 HttpContext 的异常?

盒式磁带包与 MVC4 包

ASP.net 判断页面是 http 还是 https