How to read file content while uploading

Hi Team,

  1. I want to read file contents, suppose if I am uploading a .csv file I want to read all data from it, purpose behind it to save bulk data in database through file upload.
  2. I want to customize file upload control where I want to change the label like "or drop file here" (for more than one controls in same page).
Below screenshot is for reference,

Image_5286_1732287575924

Thank you,
Kishor.


1 Reply

UD UdhayaKumar Duraisamy Syncfusion Team November 25, 2024 04:20 PM UTC

Query 1: I want to read file contents, suppose if I am uploading a .csv file I want to read all data from it, purpose behind it to save bulk data in database through file upload.


To read the contents of a .csv file uploaded using the Blazor file uploader and save bulk data in a database, you can follow these steps:
Initialize the File Uploader: Use the Blazor File Upload component to allow users to upload .csv files. Here's a basic setup:
<SfUploader AutoUpload="false">
    <UploaderEvents ValueChange="OnChange"></UploaderEvents>
</SfUploader>

Handle File Upload: Implement the OnChange event to process the uploaded file. Use a FileReader to read the contents of the .csv file.

@code {
    private void OnChange(UploadChangeEventArgs args)
    {
        foreach (var file in args.Files)
        {
            using (var reader = new StreamReader(file.Stream))
            {
                var csvContent = reader.ReadToEnd();
                // Process the CSV content
                var data = csvContent.Split('\n').Select(line => line.Split(',')).ToArray();
                // Save data to the database
                SaveDataToDatabase(data);
            }
        }
    }

    private void SaveDataToDatabase(string[][] data)
    {
        // Implement your logic to save data to the database
    }
}

Process and Save Data: Once the CSV content is read, convert it into a format suitable for your database and implement the logic to save it.
This approach uses the Blazor File Upload component to handle file uploads and a FileReader to read the contents of the uploaded .csv file. You can then process this data and save it to your database using your preferred method.

Query 2: I want to customize file upload control where I want to change the label like "or drop file here" (for more than one controls in same page).


To customize the "Drop files here" text in the Blazor Uploader control, you can use the FileDropAreaContent parameter. This can be achieved through JavaScript interop.

Here is a simple example using JavaScript interop to change the text:
@using Syncfusion.Blazor.Inputs
@using System.IO
@inject IJSRuntime JS;

<SfUploader Id="upload" AutoUpload="false" CssClass="custom">
    <UploaderEvents ValueChange="OnChange" Created="OnCreate"></UploaderEvents>
</SfUploader>

@code {
    public void OnCreate()
    {
        JS.InvokeVoidAsync("created", "upload");
    }

    private void OnChange(UploadChangeEventArgs args)
    {
        foreach (var file in args.Files)
        {
            var path = @"path" + file.FileInfo.Name;
            FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write);
            file.Stream.WriteTo(fileStream);
            fileStream.Close();
            file.Stream.Close();
        }
    }
}

<script>
    window.created = (id) => {
        document.querySelector(".custom").getElementsByClassName("e-file-drop")[0].innerText = "Custom Drop Text";
    };
</script>



Loader.
Up arrow icon