简体   繁体   中英

Upload File with IFormFile .NET Core MVC is showing error of Required File

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

在此处输入图像描述

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.

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM