We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Show Progressbar with UploaderTemplates

After implementing the  UploaderTemplates the e-upload-progress is not showing.






1 Reply

SP Sureshkumar P Syncfusion Team May 8, 2023 12:00 PM UTC

Leo,

Based on your shared information, we have created the sample. The sample will showcase the progress bar based on file size upload and also the file size displayed based on the KB/MB.

Find the code example here:

<SfUploader @ref="uploderObj" ID="UploadFiles">

                <UploaderEvents BeforeUpload="onBeforeUpload" Success="onSuccess" ValueChange="onChange"></UploaderEvents>

                <UploaderTemplates>

                    <Template>

                        @if (fileStates.ContainsKey(context.Name) && fileStates[context.Name])

                        {

                            <SfProgressBar Type="ProgressType.Linear" Height="60"

                                       Value="100" Minimum="0" Maximum="100" IsIndeterminate="true">

                                <ProgressBarAnimation Enable="true" Duration="2000" Delay="0" />

                            </SfProgressBar>

                        }

                        <div class="name file-name" title="@(context.Name)">@(context.Name)</div>

                        @if(context.Size > 0)

                        {

                           fileSize = CalculateFileSize(context.Size);

                        }

                        <div class="file-size">@fileSize</div>

                    </Template>

                </UploaderTemplates>

            </SfUploader>

 

@code {

    private SfUploader uploderObj;

    public bool isVisible { get; set; } = false;

    public string fileSize { get; set; } = "0";

 

    private Dictionary<string, bool> fileStates = new Dictionary<string, bool>();

 

    public string CalculateFileSize(double size)

    {

        string fileSizeStr = "";

        if (size >= 1024 * 1024)

        {

            return fileSizeStr = $"{size / (1024f * 1024f):0.00} MB";

        }

        else if (size >= 1024)

        {

           return fileSizeStr = $"{size / 1024f:0.00} KB";

        }

        else

        {

           return fileSizeStr = $"{size} bytes";

        }

    }

 

    public void onBeforeUpload(BeforeUploadEventArgs args)

    {

        foreach (var file in args.FilesData)

        {

            fileStates[file.Name] = true;

        }

    }

    public void onSuccess(SuccessEventArgs args)

    {

        fileStates[args.File.Name] = false;

        isVisible = false;

    }

 

    public async Task onChange(UploadChangeEventArgs args)

    {      

 

        try

        {

            foreach (var file in args.Files)

            {

                var path = @"D:\" + file.FileInfo.Name;

                FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write);

                // Calls the OpenReadStream method on the uploaded file to get a read stream

                await file.File.OpenReadStream(long.MaxValue).CopyToAsync(filestream);

                filestream.Close();

            }

        }

        catch (Exception ex)

        {

            Console.WriteLine(ex.Message);

        }

 

        isVisible = false;

        // Set the file state to false if it's not being uploaded

        foreach (var fileName in fileStates.Keys.ToList())

        {

            if (!args.Files.Any(file => file.FileInfo.Name == fileName))

            {

                fileStates[fileName] = false;

            }

        }

    }

 

}


Find the sample in the attachment:

Regards,

Sureshkumar P


Attachment: BlazorApp2_34e9efba.zip

Loader.
Up arrow icon