Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

ASP.Net Web Application using .Net Framework v5.0: IFormFile always null when passing from view to controller

I am trying to allow the user to upload a pdf file. I am not getting any errors, but the IFormFile 'PostedFile' is always null in the controller.

Create View:

 <div class="form-group">
      <label asp-for="PostedFile" class="control-label"></label>
      <div class="col-md-10">
           <input type="file" asp-for="PostedFile" />
           <span asp-validation-for="PostedFile" class="text-danger"></span>
</div>

Controller, Create method:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("ID,Name,Phone1,Phone2,District_Division,OrgNumber,DateOfTest,DateOfExposure,NumberOfExposed,Notes,PathToFile")] Case_Log case_Log, IFormFile PostedFile)
        {
            string path = "Case_Log_Docs/";
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
                System.Diagnostics.Debug.WriteLine("Created the folder.");
            }

            if (PostedFile != null)
            {
                string fileName = Path.GetFileName(PostedFile.FileName);
                System.Diagnostics.Debug.WriteLine(fileName);
                PostedFile.CopyTo(new FileStream(path, FileMode.Create));
                ViewBag.Message += string.Format("<b>{0}</b> uploaded.<br />", fileName);
            }
            else
            {
                System.Diagnostics.Debug.WriteLine("Posted file was null.");
            }

            if (ModelState.IsValid)
            {
                _context.Add(case_Log);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(case_Log);
        }

PLEASE NOTE: I (think I) do NOT want to use List as I do NOT want the user to be able to upload more than 1 single document at a time as the documents have a corresponding database entries with a 1 to 1 relationship.

I have a few questions.

1.) What is the problem? Why is IFormFile always null? 2.) Why does it seem like people always recommend List over IFormFile?

Passing the rest of the variables to the controller works fine:

<form asp-action="Create">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Phone1" class="control-label"></label>
                <input asp-for="Phone1" class="form-control" />
                <span asp-validation-for="Phone1" class="text-danger"></span>
            </div>

But the file upload div is still inside of the form that is directed to the Create method. Is there something wrong with the view element? If so how would I change it to correct the issue?

I tried following this example and got no errors but no results either: https://www.aspsnippets.com/Articles/ASPNet-Core-IFormFile-always-returns-NULL.aspx


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

1 Answer

0 votes
by (71.8m points)

You need use enctype=multipart/form-data to allows entire files to be included in the data.Like following.

<form asp-action="xxx" enctype="multipart/form-data">
 //...
    <input type="file" name="PostedFile" />
    <input type="submit" value="click"/>
</form>

Action:

[HttpPost]
public IActionResult Demo(IFormFile PostedFile)
 {
    //...
 }

Result: enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...