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

86 Views Asked by At

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));
    }

}
4

There are 4 best solutions below

0
A.nikfar On BEST ANSWER

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

2
Mike Brind On

You need to specify method="post" in your form element:

<form asp-action="BulkInsert" enctype="multipart/form-data" method="post">

1
Farhad Nosrati On

Two important points about your code:

1-Correct the spelling of lable to label.

2-Use type="submit" in the button to specify the submit type.

1
Ayush Rudani On

Use Model to pass data. so simply create a model and use this in view page

View.cshtml

<Input type="file" asp-for="File">

And also use input type submit to submit a form.

Model.cs

Public class Model{
    Public IFormFile File {get; set;}
}

And in controller put model as parameter

Controller.cs

Public Action (Model model){
  // you get file here using model.File
}

Try this way....