GridEditSettings Template does not refresh Image with source change

Hello,

I am trying to set image for a database byte array field in GridEditSettings Template using InputFile control. I have tried SfUploader too, but because i could not hide Template i have decided to use Microsoft.AspNetCore.Components.Forms InputFile control.  The following snipped shows what i want to do:

<SfGrid TValue="Client">
    <GridEditSettings AllowAdding="true" AllowEditing="true" AllowDeleting="true" ShowDeleteConfirmDialog="true" Mode="EditMode.Dialog">
        <HeaderTemplate>
            @{
                <span>Client</span>
            }
        </HeaderTemplate>
        <Template>
            @{
                var entity = (context as Client);
                ImageSourse = string.Format("data:image/png;base64,{0}", entity.Logo != null ? Convert.ToBase64String(entity.Logo) : null);
                
                <div>
                    <div class="form-group col-md-6">
                        <label>Logo</label>

                        <img src=@ImageSourse height="35" />
                        <InputFile accept="png" OnChange="@(async (ea) => { entity.Logo = await GetFileContentAsync(ea.GetMultipleFiles(1).SingleOrDefault()); })" />
                    </div>
                </div>
            }
        </Template>
    </GridEditSettings>
    <SfDataManager Url="odata/Clients" Adaptor="Adaptors.ODataV4Adaptor" />
    
    <GridColumns>
        <GridColumn Field=@nameof(Client.Logo) HeaderText="Logo" AutoFit="true">
            <Template>
                <img src="@string.Format("data:image/png;base64,{0}", (context as Client).Logo != null ? Convert.ToBase64String((context as Client).Logo) : null)" height="35" />
            </Template>
        </GridColumn>
    </GridColumns>
</SfGrid>

@code {
    public string ImageSourse { get; set; }

    public async Task<byte[]> GetFileContentAsync(IBrowserFile browserFile)
    {
        byte[] fileContent = null;

        if (browserFile != null)
        {
            try
            {
                using System.IO.Stream stream = browserFile.OpenReadStream();
                {
                    fileContent = new byte[browserFile.Size];
                    await browserFile.OpenReadStream().ReadAsync(fileContent);
                    ImageSourse = string.Format("data:image/png;base64,{0}", Convert.ToBase64String(fileContent));
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }
        }

        return fileContent;
    }

    public partial class Client
    {
       
        public byte[] Logo { get; set; }

        //... other fields
    }
}

How can i refresh Logo image as soon as i select an image file on Edit Dialog so user can see new selected image before saving.

Actually i don't want to show upload file or select file button but for now its ok, if it was possible to show a tooltip on image space when user hover over with message "select image" and if user clicks on image (image space) then file selector dialog opens, this can be a feature request. It will be great if you can add an image selector that works as explained.

Thanks.





5 Replies 1 reply marked as answer

RS Renjith Singh Rajendran Syncfusion Team April 8, 2021 12:29 PM UTC

Hi Ali, 

Greetings from Syncfusion support. 

Query 1 : How can i refresh Logo image as soon as i select an image file on Edit Dialog so user can see new selected image before saving. 
We suggest you to ensure to disable the PreventRender inside the OnActionComplete event of Grid to overcome the reported behavior you are facing. Please refer and add the below codes in your application, 

<GridEvents OnActionComplete="OnActionComplete" TValue="Client"></GridEvents>
 
public void OnActionComplete(ActionEventArgs<Client> args) 
{ 
    if (args.RequestType.Equals(Action.Add) || args.RequestType.Equals(Action.BeginEdit)) 
    { 
        args.PreventRender = false; 
    } 
} 


We have also prepared a sample for your convenience, please download the sample from the link below. Below same contains the both Query 1 and Query 2 suggestions. 

Query 2 : if it was possible to show a tooltip on image space when user hover over with message "select image" and if user clicks on image (image space) then file selector dialog opens, this can be a feature request. It will be great if you can add an image selector that works as explained. 
We suggest you to use SfToolTip to display tooltip on hovering the image space. And in @onclick event handler, we have used Microsoft JsInterop to programmatically trigger the InputFile triggering action to open the file selection window. Please refer the codes below. We have also included this solution in the above attached sample. 

 
<Template> 
    @{ 
        ...  
        <div> 
            <div class="form-group col-md-6"> 
                <label>Logo</label> 
                <SfTooltip Target="#img" Content="Select Image" @onclick="onclick"> 
                    <img id="img" src=@ImageSourse height="35" /> 
                </SfTooltip> 
                <InputFile accept="png" ... /> 
            </div> 
        </div> 
    } 
</Template> 

public async Task onclick() 
{ 
    await JsRuntime.InvokeAsync<object>("openbrowse"); 
} 

[openbrowse.js] 

function openbrowse() { 
    document.querySelector('input[type=file]').click(); 
 

 

Please get back to us if you need further assistance. 

Regards, 
Renjith R 


Marked as answer

AA ali arslan April 8, 2021 08:29 PM UTC

Thank you Renjith,

Both image refresh and image selector works fine.




I hide (style="display: none") InputFile, and now it is how i wanted.

If you can add a simple component working like this in your framewrok (SfImageEditor - SfImageSelector), it will be very handy for programmers that use database images.

Regards.
Ali


NR Nevitha Ravi Syncfusion Team July 28, 2021 09:28 AM UTC

Hi Ali, 
 
Currently we don’t have ImageEditor control. We have already logged this as a feature request. We will implement this control in any one of our upcoming releases. You can track the feature status from the below link. 
 
 
At the planning stage for every release cycle, we review all open features and identify features for implementation based on specific parameters including product vision, technological feasibility, and customer interest and its importance in component. Priority will be given to feature request which has maximum number of votes. So, we request you to keep track of the status through the above feedback link and let you know once the feature will be scheduled. 
 
Regards, 
Nevitha 



KA kavit November 25, 2022 10:04 PM UTC

It is possible to do the same by replacing InputFile on SfUploader?



JL Joshna Lingala Uthama Reddy Lingala Syncfusion Team November 29, 2022 01:38 PM UTC

Hi Kavit,


Greetings from Syncfusion.


As per the requirement, we have prepared a sample by replacing the inputFile with the Syncfusion File Upload component and ValueChange event. Refer the below code snippets


<Template>

    @{

        var entity = (context as Client);

        ImageSourse = string.Format("data:image/png;base64,{0}", entity.Logo != null ? Convert.ToBase64String(entity.Logo) : null);

        <div>

            <div class="form-group col-md-6">

                <label>Logo</label>

                <SfTooltip Target="#img" Content="Select Image" @onclick="onclick">

                    <img id="img" src=@ImageSourse height="35" />

                </SfTooltip>

                <SfUploader AllowedExtensions=".png" AllowMultiple="false">

                    <UploaderEvents ValueChange="@(async (args) => {

                                            var file = args.Files[0];

                                            byte[] Logo = file.Stream.ToArray();

                                            entity.Logo = Logo; })">

                    </UploaderEvents>

                </SfUploader>

            </div>

        </div>

    }

</Template>

 

 


However, we have prepared sample for your reference which you can download from the below link


Sample :  https://www.syncfusion.com/downloads/support/directtrac/general/ze/ServerApp_Grid_uploader695162192


Regards,

Joshna L


Loader.
Up arrow icon