0

After Selecting the file form Choos File, submitting the form showing error "The formFile field is required."

enter image description here

Model is here: In Debug mode formFile value is set; it shows null;

    public class FileUploadTest
    {
        [Key]
        public Guid Id { get; set; }
        public string Name { get; set; }
        [NotMapped]
        public IFormFile formFile { get; set; }
        public string fromFileUrl { get; set; }
    }

View is here:

@model UdyogWeb.Models.FileUploadTest

@{
    ViewData["Title"] = "Create";
}
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create" asp-controller="FileUploadTests" enctype="multipart/form-data" method="post">
            <div asp-validation-summary="All" 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="formFile" class="control-label">Upload File</label>
                <input asp-for="formFile" class="form-control" />
                <span asp-validation-for="formFile" class="text-danger"></span>
            </div>
           @* <div class="form-group">
                <label asp-for="fromFileUrl" class="control-label"></label>
                <input asp-for="fromFileUrl" class="form-control" />
                <span asp-validation-for="fromFileUrl" class="text-danger"></span>
            </div>*@
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-action="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

Controller is here:

[HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Name,fromFileUrl")] FileUploadTest fileUploadTest)
        {
            string folderurl = " ";
            if (ModelState.IsValid)
            {
                if (fileUploadTest.formFile != null)
                {

                    folderurl = "casefile/brieforwrit/";
                    folderurl += Guid.NewGuid().ToString() + fileUploadTest.formFile.FileName;
                    //fileUploadTest.fromFileUrl = folderurl;
                    string serverfolderurl = Path.Combine(_webHostEnvironment.WebRootPath, folderurl);
                    using (FileStream fs = new FileStream(serverfolderurl, FileMode.Create))
                    {
                        await fileUploadTest.formFile.CopyToAsync(fs);
                    }

                }
                fileUploadTest.fromFileUrl = folderurl;
                fileUploadTest.Id = Guid.NewGuid();
                _context.Add(fileUploadTest);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(fileUploadTest);
        }

In this project, I uploaded some files on the Identity Rejor page and it's working perfectly.

Please help to solve this error Thank you so much for your attention and participation.

1
  • Thank you for your reply. But I have tried once again on the same project on a different pc also *shows the same error * Commented Jul 22, 2022 at 11:46

1 Answer 1

1

In Debug mode formFile value is set; it shows null;

You need to bind your formFile too.

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Create([Bind("Id,Name,fromFileUrl,formFile")] FileUploadTest fileUploadTest)
        {...}

result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.