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

Update UploderFiles at runtime

Hi,

we would like to update the file list at runtime - tried all functions of SfUploader but none has any effect on the visual file list. ( also tested suggestions from https://www.syncfusion.com/forums/175654/display-uploaded-files-using-a-parameter-in-sfuploader but doesnt work at runtime )

our use case is to show the existing file in the file list, so the user can delete it there - this entry has to be added / updated on button click


5 Replies 1 reply marked as answer

SP Sureshkumar P Syncfusion Team December 21, 2022 08:19 AM UTC

Hi Christoph,

Thank you for reaching out to us regarding the issue you are experiencing with the dynamic support not working when updating the preload files in the component. You can try using conditional rendering to initialize the component. This may resolve the issue you are experiencing.

Find the code example here:

<div class="dropElement">

 

    @if (files != null)

    {

        <SfUploader AutoUpload="false" DropArea=".dropElement">

            <UploaderFiles>

                @if (files != null && files.Count > 0)

                {

                    @foreach (var item in files)

                    {

                        <UploaderUploadedFiles Name="@item.Name" Size=@item.size Type="@item.type"></UploaderUploadedFiles>

                    }

 

                }

            </UploaderFiles>

            <UploaderEvents ValueChange="OnChange"></UploaderEvents>

        </SfUploader>

    }

</div>

 

@code {

    public List<FileList> files { get; set; }

 

    public class FileList

    {

        public string Name { get; set; }

        public double size { get; set; }

        public string type { get; set; }

    }

 

    protected override void OnInitialized()

 

    {

 

        this.files = new List<FileList>()

        {

            new FileList() { Name = "Chocolate", size=500000, type= ".pdf" },

            new FileList() { Name = "Chocolate2", size=500000, type= ".pdf" },

        };

 

        base.OnInitialized();

 

    }

 

    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();

        }

    }

}


Regards,

Sureshkumar P



CH Christoph December 21, 2022 08:31 AM UTC

well this doesnt work - it only works if you do it in OnInitialized but i need to update it on runtime


Attachment: SfUploader_48d339b1_a77e42a0.zip


if you click on the button, its added to list but SfUploader is not updated



SP Sureshkumar P Syncfusion Team December 22, 2022 11:26 AM UTC

Christoph, As mentioned in our previous update, the preload feature does not support the dynamic updating of files. This is the default behavior of the component.



CH Christoph December 22, 2022 12:34 PM UTC

we notice that it can achived by using CreateFileList - here is an example


<button onclick="@AddEntry">Add Entry</button>
<button onclick="@Clear">Clear</button>

<SfUploader @ref="_uploader" />

@code{
private readonly List<FileInfo> _files = new();
private SfUploader _uploader;


private async Task AddEntry()
{
_files.Add( new FileInfo()
{
Name = $"Added {DateTime.Now.Ticks}",
Status = "File uploaded successfully",
ValidationMessages = new ValidationMessages()
});


await _uploader.CreateFileList( _files.ToArray() );
}


private async Task Clear()
{
_files.Clear();
await _uploader.ClearAll();
}
}


note: Status is not required for FileInfo but has be to one of the following:

File failed to upload,

Ready to upload,

default is: File type is not allowed


other status can be achived by setting size to <min or >max


Marked as answer

SS Shereen Shajahan Syncfusion Team December 23, 2022 06:45 AM UTC

Glad to know the issue has been resolved. Please get back to us for further assistance.


Loader.
Live Chat Icon For mobile
Up arrow icon