为什么 IFormFile 显示为空,我该如何解决? [英] Why is IFormFile showing null, and how do I fix it?

查看:31
本文介绍了为什么 IFormFile 显示为空,我该如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 VS 2015 中使用 ASP.NET 5、MVC 6.我正在制作一个 Web 应用程序.在我的表格上,我有:

 
<div id="uploadSection" ><label >选择要上传的文件</label><input type="file" id="fileSelect" name="fileSelect" 多个/>

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

</表单>

在我的控制器中:

 [HttpPost]公共异步任务FileForm(FileViewModel vm, IFormFile 文件){如果(模型状态.IsValid){//IFormFileCollection files = Request.Form.Files;string filePath = Path.Combine("C:", "transfers");//foreach(文件中的var文件)//{if (file != null && file.Length > 0){var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');等待 file.SaveAsAsync(Path.Combine(filePath, fileName));}//}ModelState.Clear();}返回视图();}

我有一个更大的表单,其中包含要进入我的 FormViewModel 的母表单字段(所有字段都按原样填充),实际上我尝试将 IFormFileCollection、ICollection、IList、List 作为方法中的参数上面,而是将它直接放在我的视图模型中.无论我是作为单个文件还是多个文件,在模型或控制器中,我总是得到空值.'IFormFileCollecion files = Request.Form.Files' 实际上有效,但我不想那样做,因为它应该按照我拥有或拥有的方式工作.当我将文件的属性放入我的模型时,我将[Required]"放在它上面并且它不会触发.

而且,更好的是,我用 fiddler 运行了多次,它实际上是在发布文件——那么为什么不将它们投射到模型中呢?

我的依赖关系错了吗?谁能想到是什么导致了这种情况?

 "依赖项": {"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"的名称属性.您的操作参数的名称是文件".使它们匹配,它应该可以工作.

I am using ASP.NET 5, MVC 6 in VS 2015. I am making a web application. On my form I have:

    <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>

in my controller:

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

I have a much larger form with mother form fields in it that are going into my FormViewModel (ALL fields are populating as they should), and I actually tried putting the IFormFileCollection, ICollection, IList, List as the parameters in the method above and instead also putting it directly in my view model. Whether I do it as a single file or multiple, in the model or the controller, I always get null. The 'IFormFileCollecion files = Request.Form.Files' actually works, but I don't want to do it that way because it should work the way I have or had it. When I put the property for the files in my model, I put "[Required]" above it and it doesn't trigger.

AND, even better, I ran this multiple times with fiddler and it is actually POSTING the files-- so why aren't they being cast into the model?

Do I have my dependencies wrong? Can anyone think of what could cause this?

  "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"
  },

解决方案

Your file input has a name attribute of "fileSelect". The name of your parameter to your action is "file". Make them match and it should work.

这篇关于为什么 IFormFile 显示为空,我该如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆