asp.net-core-mvcvisual-studio-2022asp.net-core-6.0iformfile

IFormFile passing null value to Action in asp.netcore RazorPage 6.0 using vs2022


i created a dotnetCore6.0 MVC RazorePage solution using VisualStudio2022 , i have a page that give a excel file and will be insert into database , unfortinantly whenever trying to send the file, it is NULL in the action ! also my httpcontext.Request.Form Has a Error! here is my cshtml :

<div class = "card">
    <div class="card-header"> Upload Products Excel File   </div>
    <div class="card-body" >
        <form asp-action="BulkInsert"  enctype="multipart/form-data"  >
            <div class="form-group" >
                <lable> Excel File </lable>
                <input name="excelFile" id="excelFile" type="file" accept=".xlsx, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" />
            </div>
            <div class="form-group">
                <button class="btn-primary btn-success" >
                    Send File
                </button>
            </div>

        </form>

    </div>
</div>

and this is the cs method

[HttpPost]
    public async Task<IActionResult> BulkInsert(IFormFile excelFile)
    {
        // First Add Package  'dotnet add package ExcelDataReader' 
        // then using MemoryStream to read Excel data

        var formFile = HttpContext.Request.Form.Files[0];
        var aa = HttpContext;
        List<ProductViewModel> products = new List<ProductViewModel>();

        using (var ms = new MemoryStream())
        {
            excelFile.CopyTo(ms);
            bool firstRow = true;

            using (var reader = ExcelReaderFactory.CreateReader(ms))
            {
                do
                {
                    while (reader.Read())
                    {
                        if (firstRow)// Breakes First Row
                        {
                            firstRow = false;
                            continue;

                        }
                        var product = new ProductViewModel()
                        {
                            ProductName = reader[0].ToString(),
                            CategoryID = Convert.ToInt32(reader[1]),
                            SupplierID = Convert.ToInt32(reader[2]),
                            UnitPrice = Convert.ToDouble(reader[3]),
                        };
                        products.Add(product);
                    }
                    
                } 
                while(reader.NextResult());
            }

        }
        return View(nameof(Index));
    }

}

Solution

  • it's really funny i implemented this code at my workplace and forgot to put them to github ! now i implementing the project at home again and everything works exactly correct !!!!!! think i installed a wrong Package at workplace . here is the package that i instaled at home.

    ExcelDataReader v3.6.0

    instaled more than 63.3M