Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
757 views
in Technique[技术] by (71.8m points)

asp.net - Why is IFormFile showing null, and how do I fix it?

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"
  },
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...