Upload file from grid editing mode

Hello,

I have a grid with multiple column, and I have a column that I want to use to store the file path.

I want to be able to upload the file from the dialog edit mode like image bellow


I want that the file will be uploaded to the server side after clicking on Add.

Thanks a lot for your help


3 Replies

RN Rahul Narayanasamy Syncfusion Team January 10, 2022 01:30 PM UTC

Hi Anass, 

Greetings from Syncfusion. 

You want to upload an image while adding/editing and show that image in Grid. We suggest you to achieve your requirement by using Column Template(for show image in Column) and EditTemplate feature(To render SfUploader for uploading the image while adding/editing) of the Grid.   

Here, we have shown the image in Grid columns using Column Template. For uploading the image, we have used EditTemplate. Find the below code snippets for your reference.  

 
<SfGrid AllowPaging="true" DataSource="@Orders" Toolbar="@(new List<string>() { "Add", "Edit", "Delete", "Cancel", "Update" })"> 
    <GridEvents OnActionBegin="BeginHandler" TValue="Order"></GridEvents> 
    <GridEditSettings AllowEditing="true" AllowDeleting="true" AllowAdding="true"></GridEditSettings> 
    <GridColumns> 
        <GridColumn Field=@nameof(Order.OrderID) HeaderText="Order ID" IsPrimaryKey="true" TextAlign="@TextAlign.Center" Width="140"></GridColumn> 
        <GridColumn Field=@nameof(Order.Freight) HeaderText="Freight" EditType="EditType.NumericEdit" Format="C2" Width="140" TextAlign="@TextAlign.Right"></GridColumn> 
        <GridColumn Field="Imagesrc" HeaderText="Customer Name" Width="200"> 
            <Template>          @*for showing the image in Grid column*@ 
                @{ 
                    var imageUrl = (context as Order).Imagesrc; 
                    <div class="image"> 
                        <img src="@imageUrl" /> 
                    </div> 
                } 
            </Template> 
            <EditTemplate>     @*for rendering SfUploader component in EditTemplate for uploading the image during Adding/Editing*@ 
                <SfUploader ID="uploadFiles" AllowedExtensions=".jpg,.png,.jpeg" Multiple="false"> 
                    <UploaderEvents FileSelected="Selected" ValueChange="OnChange"></UploaderEvents> 
                </SfUploader> 
            </EditTemplate> 
        </GridColumn> 
    </GridColumns> 
</SfGrid> 
 
@code{ 
    public int? DefaultValue = 100; 
    public string UploadedFile { get; set; } 
    public void BeginHandler(ActionEventArgs<Order> Args) 
    { 
        if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save && Args.Action == "Add") 
        { 
            //Args.Data.OrderID = DefaultValue++;    //set the default value while adding. 
            //save the file name / url in grid datasource / your database. You can generate the byte and store here. 
            Args.Data.Imagesrc = "scripts/Images/Employees/"+UploadedFile; 
        } else if (Args.RequestType == Syncfusion.Blazor.Grids.Action.Save && Args.Action == "Edit") 
        { 
            //save the file name / url in grid datasource. You can generate the byte and store here. 
            Args.Data.Imagesrc = "scripts/Images/Employees/" + UploadedFile; 
        } 
 
    } 
    public void Selected(SelectedEventArgs Args) 
    { 
        UploadedFile = Args.FilesData[0].Name; 
    } 
 
    public void OnChange(UploadChangeEventArgs args) 
    { 
        foreach (var file in args.Files) 
        { 
            var size = file.FileInfo.Size; 
            var path = @"./wwwroot/scripts/Images/Employees/" + file.FileInfo.Name; 
            FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write); 
            file.Stream.WriteTo(filestream); 
            filestream.Close(); 
            file.Stream.Close(); 
        } 
    } 
    . . . 
} 




Reference

Please let us know if you have any concerns. 

Regards, 
Rahul  



AN Anass January 12, 2022 01:32 PM UTC

Thank you very much. that's what I'm looking for



RN Rahul Narayanasamy Syncfusion Team January 13, 2022 05:02 AM UTC

Hi Anass, 

Thanks for the update. 

We are happy to hear that the provided solution was helpful to achieve your requirement. Please get back to us if you need further assistance. 

Regards, 
Rahul 


Loader.
Up arrow icon