Delete Files

Hello,
I upload documents to the server using the RadzenUpload Component. The files are saved in wwwroot and the path to the document is saved in DB.

Now I want to delete the files too, but I can't get it to work.
I extend the DocumentsController to OnAfterCtDocumentDeleted in a CustomController:

partial void OnAfterCtDocumentDeleted (Models.Idlinkeddata.CtDocument item) {

        if (System.IO.File.Exists (item.Document))
        {
            System.IO.File.Delete (item.Document);
        }

-> the Method is called in public IActionResult DeleteCtDocument(int key), but does nothing

I also tried implementing the method in the XXX.razor.cs(client, not server) file as a custom method that I can invoke in radzen on ButtonClickEvent:

        private void DeleteFile (string filePath)
    {
        
        if (System.IO.File.Exists (filePath))
        {
            System.IO.File.Delete (filePath);
        }
    }

-> but the method is not called.

Do you have any idea why it's not working? Is there any other way to delete files?

Thank you!

You should try debugging the application to inspect if item.Document contains the right value. It probably does not at the moment and File.Exists always returns false.

Hello,
the item.Document is always something like: http://localhost:5000/7/files.xlsx. I use it to download the files.

Then File.Exists will never return true. You need to store the physical path of your file as well.

Yes, that was the problem. It works with physical path. Thank you!