locked
Read .csv file in asp.net core 3.1 razor , selecting the file from a file uploader control but not uploading the same RRS feed

  • Question

  • User-460608850 posted

    How to read rows from .csv file in asp.net C# core 3.1 razor , selecting the file from a file uploader control but not uploading the same

    Friday, November 13, 2020 2:30 PM

All replies

  • User1312693872 posted

    Hi,rajibsaha81

    I made a demo for you, you can get the rows like the following method:

    public async Task<IActionResult> OnPostReadFileAsync(IFormFile file)
            {
                if (file.FileName.EndsWith(".csv"))
                {
                    using (var sreader = new StreamReader(file.OpenReadStream()))
                    {
                      string[] headers = sreader.ReadLine().Split(','); //Title
                      while (!sreader.EndOfStream) //get all the content in rows
                        {
                            string[] rows = sreader.ReadLine().Split(',');
                            int Id = int.Parse(rows[0].ToString());
                          int NUM = int.Parse(rows[1].ToString()); ...... } } else { .... } }

    Result:

    Feel free to contact me if you have any questions.

    Best Regards,

    Jerry Cai

    Monday, November 16, 2020 6:49 AM
  • User-460608850 posted

    Thank you for the help.  in my code  IFormFile file , file is returning null result. Pls assist . 

    Tuesday, November 17, 2020 8:08 AM
  • User1312693872 posted

    Hi,rajibasaha81

    Can you offer your related code? It can help me reproducing your problem.

    Best Regards,

    Jerry Cai

    Tuesday, November 17, 2020 9:40 AM
  • User-460608850 posted

    Hi Jerry,

    Thank you for the reply. Here is the code. I am a beginner in working with dotnet core razor 3.1  

    in UploadCode.cshtml 

    <form method="post" enctype="multipart/form-data">
        <input id="postedFile" type="file" accept=".csv" name="postedFile" asp-for="Upload" runat="server" />    <input type="submit" value="UploadCode" />    <div asp-validation-summary="ModelOnly" style="color:red"></div>
        <span asp-validation-for="Upload" style="color:red"></span>
        <div><input type="submit" value="InsertCode" asp-page-handler="InsertCode" runat="server" /></div></form>

    in UploadCode.cshtml.cs

    [HttpPost]
            public async Task<IActionResult> OnPostInsertCodeAsync(IFormFile upload)
            {
                           
                if (upload.FileName.EndsWith(".csv"))
                {
                    using (var sreader = new StreamReader(upload.OpenReadStream()))
                    {
                        string[] headers = sreader.ReadLine().Split(',');     //Title
                        while (!sreader.EndOfStream)                          //get all the content in rows 
                        {
                            string[] rows = sreader.ReadLine().Split(',');
                            int Id = int.Parse(rows[0].ToString());
                            int NUM = int.Parse(rows[1].ToString());

                        }
                    }
                }

                return null;

    }

    Tuesday, November 17, 2020 2:35 PM
  • User1312693872 posted

    Hi,rajibsaha81

    You missed asp-page-handler, and I recommend you put the insertcode out of the form, if they handle different actions.

    <form method="post" enctype="multipart/form-data" asp-page-handler="ReadFile">
        <input id="postedFile" type="file" accept=".csv" name="postedFile" asp-for="Upload" runat="server" />
        <input type="submit" value="UploadCode" />
        <div asp-validation-summary="ModelOnly" style="color:red"></div>
        <span asp-validation-for="Upload" style="color:red"></span>
    </form>
    
    <div>
        <input type="submit" value="InsertCode" asp-page-handler="InsertCode" runat="server" />
    </div>

    Best Regards,

    Jerry Cai

    Wednesday, November 18, 2020 3:01 AM
  • User-460608850 posted

    HI Jerry,

    I am facing the same issue. 

    IFormFile upload ... upload is still null.

    Thanks,

    Rajib

    Wednesday, November 18, 2020 1:48 PM
  • User1312693872 posted

    Hi,rajibsaha21

    I assume you want to choose a file and then you can decide upload or insertcode according to this file? Then you can use the following, each

    page handler will handle specific actions, make sure the 'asp-for=***' can match your 'IFormfile ***'

    <form method="post" enctype="multipart/form-data">
        <input type="file" accept=".csv" asp-for="Upload" runat="server" />
        <input type="submit" value="UploadCode" asp-page-handler="ReadFile"/> 
        <div asp-validation-summary="ModelOnly" style="color:red"></div>
        <span asp-validation-for="Upload" style="color:red"></span>
        <div>
            <input type="submit" value="InsertCode" asp-page-handler="InsertCode" />
        </div>
    </form>

    Best Regards,

    Jerry Cai

    Thursday, November 19, 2020 2:01 AM
  • User-460608850 posted

    Thank you Jerry. Will try and let you know. 

    Thanks,

    Rajib

    Thursday, November 19, 2020 2:37 PM
  • User1312693872 posted

    Hi,rajibsaha81

    Do you have any problem? Welcome to share it.

    Best Regards,

    Jerry Cai

    Monday, November 23, 2020 1:17 AM
  • User-460608850 posted

    Hi Jerry,

    We are not able to perform the action using the file loader control. So we have decided to upload the file directly in Azure and load the data in the table.  Thank you for your assistance Jerry.  If we require to comeback on this approach , I will keep this post active .

    Many Thanks,

    Rajib

    Wednesday, November 25, 2020 6:55 AM