Hello Team,
How do I delete my uploaded document from folder directory in my ASP.net MVC project
Hello Team,
How do I delete my uploaded document from folder directory in my ASP.net MVC project
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Vishal JoshiPosted Mar 7, 2024, 5:05 AM
Hello Emmanuel
Please checkout the below code to delete/remove the uploaded files from server directory.
Controller Code
View Code
Thanks
Vishal Joshi
Rohini ParadePosted Mar 7, 2024, 4:42 PM
You can use
string directory = @"C:\temp";
string filename = @"readme1.txt";
try {
File.Delete(Path.Combine(directory, filename));
} catch (IOException ex) { WriteLine(ex.Message); }
Emmmanuel FIADUFEPosted Mar 7, 2024, 11:15 AM
Thank you team, it is working now
Jayraj ChhayaPosted Mar 7, 2024, 9:15 AM
To delete an uploaded document from a folder directory in an ASP.NET MVC project, you can use the
System.IO.Fileclass to perform the deletion operation. Here's a simple example of how you can achieve this:In your controller or service class, you can call the
DeleteFilemethod by passing the file path as a parameter to delete the uploaded document from the folder directory.Naimish MakwanaPosted Mar 7, 2024, 7:20 AM
here’s a simple example of how you might set up a controller and view to delete a file in an ASP.NET MVC application.
In this controller, there’s an action
DeleteFilethat takes afileNameas a parameter. It constructs the file path, checks if the file exists, and if it does, deletes the file. The result of the operation is stored inViewBag.Message.View (DeleteFile.cshtml):
you can create a link in your view that triggers the
DeleteFileaction when clicked. Here’s an example:Thanks
Jignesh KumarPosted Mar 7, 2024, 3:48 AM
add link to delete in .cshtml
Andrew BhaurPosted Mar 7, 2024, 1:26 AM
When a user or staff member uploads a file, one technique to handle multiple file extensions is to first save the filename together with the extension in the relevant database table. When deleting the file, retrieve the same and use it. If you decide you no longer need the folder, you can delete it. Make sure you won't be impacting any functionality that depends on the contents of that folder, though, before proceeding. Before deleting a folder, always create a backup in case you need to restore it later. To make sure that the deletion won't result in any problems, think about speaking with your team or a more knowledgeable developer if the folder is a part of a bigger project.