File Upload - Files are persisted following upload

Hello

I have an uploader like this

        <SfUploader  Multiple="true" SequentialUpload="true" ShowFileList="false" AutoUpload="true" AllowedExtensions=".pdf,.PDF">
            <UploaderEvents ValueChange="UploadReceipts"></UploaderEvents>
        </SfUploader>

If I process say 5 files then this works as expected.

If I then process another 2 I can see that there are now 7 files in the args.Files (UploadChangeEventArgs ).

I clearly need to remove the files from the previous upload, but given that I have ShowFileList set to false, how do I achieve this.

Thanks

7 Replies 1 reply marked as answer

SN Sevvandhi Nagulan Syncfusion Team August 26, 2020 07:36 AM UTC

Hi Richard, 


Greetings from Syncfusion support. 


We would like to notify you that we will provide the entire file list which we uploaded to the uploader component in the change event argument. This is control’s intended behaviour. Please provide the details on why the previous files should be removed. We will check other possibilities based on the information to achieve your requirement. 

Regards, 
Sevvandhi N 



DI Ditchford August 26, 2020 08:05 AM UTC

Hello

Thanks for the feedback.  The workflow is as follows:

Upload some files.  Say 5
These files are saved to the database and records representing these files are displayed in a data grid on the same page
Once the user is happy that these files have been uploaded then he may now wish to upload another say 10 files
As it stands at the moment on uploading the next 10 files he will actually get 15 files.

What I need to do is programmatically remove the first 5 files, in the same way that a user might manually remove them using the FileList if "ShowFileList" was set to true.

Thanks




SN Sevvandhi Nagulan Syncfusion Team August 27, 2020 01:39 PM UTC

Hi Richard, 


You can delete the already uploaded file in the server using the public remove method which will trigger the OnRemove event where you can delete the file. Refer to the code below, 

public void UploadReceipts(UploadChangeEventArgs args) { 
        if (files != null && files.Count > 0) 
        { 
            foreach (var filee in files) 
            { 
                UploaderObj.Remove(filee); 
            } 
        } 
        files = new List<Syncfusion.Blazor.Inputs.FileInfo>(); 
        foreach (var file in args.Files) 
        { 
            files.Add(new Syncfusion.Blazor.Inputs.FileInfo() {Name = file.FileInfo.Name ,Id= file.FileInfo.Id,RawFile = file.FileInfo.RawFile, Size= file.FileInfo.Size, Status= file.FileInfo.Status, StatusCode= file.FileInfo.StatusCode, FileSource = file.FileInfo.FileSource , Input= file.FileInfo.Input, List = file.FileInfo.List , Type = file.FileInfo.Type , ValidationMessages = file.FileInfo.ValidationMessages 
            }); 
            var path = file.FileInfo.Name; 
            FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write); 
            file.Stream.WriteTo(filestream); 
            filestream.Close(); 
            file.Stream.Close(); 
        } 
 
 
    } 
    private void onRemove(RemovingEventArgs args) 
    { 
        foreach (var removeFile in args.FilesData) 
        { 
            if (File.Exists(Path.Combine(@"rootPath", removeFile.Name))) 
            { 
                File.Delete(Path.Combine(@"rootPath", removeFile.Name)); 
            } 
        } 
    } 

Please find the sample below, 



Regards, 
Sevvandhi N 


Marked as answer

DI Ditchford August 30, 2020 02:49 AM UTC

Excellent. Thank you


SN Sevvandhi Nagulan Syncfusion Team August 31, 2020 10:56 AM UTC

Hi Richard,

We are glad to hear that our solution got worked. Please get back to us if you have any queries.

Regards,
Sevvandhi N
 



MG Miles Gibson February 18, 2021 09:46 PM UTC

I downloaded this sample application, and it worked until I upgraded the Synfusion.Blazor tools to the latest .48 version.  Now I get this error:

Error CS1503 Argument 1: cannot convert from 'Syncfusion.Blazor.Inputs.FileInfo' to 'Syncfusion.Blazor.Inputs.FileInfo[]' BlazorApp1 C:\Users\super\Downloads\RemoveFiles1923195416\BlazorApp1\BlazorApp1\Pages\Index.razor 23 Active

What now?


SN Sevvandhi Nagulan Syncfusion Team February 19, 2021 03:09 PM UTC

Hi Miles, 


We checked your query. Please find the code for removing the file. 


    SfUploader UploaderObj; 
    Syncfusion.Blazor.Inputs.FileInfo[] files = new Syncfusion.Blazor.Inputs.FileInfo[20]; 
    public int count { get; set; } = 0; 
    public void UploadReceipts(UploadChangeEventArgs args) { 
        files[count] = new Syncfusion.Blazor.Inputs.FileInfo(); 
        foreach (var file in args.Files) 
        { 
                   var path = file.FileInfo.Name; 
            FileStream filestream = new FileStream(path, FileMode.Create, FileAccess.Write); 
            file.Stream.WriteTo(filestream); 
            filestream.Close(); 
                  files[count].Name = file.FileInfo.Name; 
            files[count].Id = file.FileInfo.Id; 
            files[count].RawFile = file.FileInfo.RawFile; 
            files[count].Size = file.FileInfo.Size; 
            files[count].Status = file.FileInfo.Status; 
            files[count].FileSource = file.FileInfo.FileSource; 
            files[count].List = file.FileInfo.List; 
            files[count].Type = file.FileInfo.Type; 
            files[count].ValidationMessages = file.FileInfo.ValidationMessages; 
            count = count + 1; 
            file.Stream.Close(); 
        } 
 
 
    } 
 
    public void Remove() 
    { 
           
                UploaderObj.Remove(files); 
          
    } 


Please check the above modified code and get back to us if you need further assistance. 


Regards, 

Sevvandhi N 


Loader.
Up arrow icon