我在VS 2015中使用ASP.NET 5,MVC 6.我正在制作一个Web应用程序.在我的表格上我有:
<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(所有字段都填充它们应该),我实际上尝试将IFormFileCollection,ICollection,IList,List作为上述方法中的参数,而是也直接把它放在我的视图模型中.无论我是作为单个文件还是多个文件,在模型或控制器中,我总是得到null.
‘IFormFileCollecion files = Request.Form.Files’实际上有效,但我不想这样做,因为它应该按照我拥有或拥有它的方式工作.
当我把文件的属性放在我的模型中时,我把“[required]”放在它上面并且它不会触发.
而且,更好的是,我用fiddler多次运行它,它实际上是POST文件 – 所以为什么不将它们投射到模型中?
我的依赖关系错了吗?谁能想到可能导致这种情况的原因?
"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" },