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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Vishal JoshiRank #377 · 6.7K reputationPosted Mar 7, 2024, 12:05 AM EST
Hello Emmanuel
Please checkout the below code to delete/remove the uploaded files from server directory.
Controller Code
public ActionResult DeleteFile(string fileName) { string fullPath = Request.MapPath("~/softCopyFiles/" + fileName); if (System.IO.File.Exists(fullPath)) { System.IO.File.Delete(fullPath); return RedirectToAction("FileList"); } else { return Content("File does not exists!"); } }View Code
Thanks
Vishal Joshi
Jignesh KumarRank #91 · 22.9K reputationPosted Mar 6, 2024, 10:48 PM EST
public ActionResult DeleteFile(string fileName) { // Your uploaded file folder directory string folderPath = Server.MapPath("~/Uploads"); // Create combinpath of folder and filename string filePath = Path.Combine(folderPath, fileName); try { // Check if the file exists or not using File.Exists method if (System.IO.File.Exists(filePath)) { // Delete the file using File.Delete method System.IO.File.Delete(filePath); // return a success message or custom message you want to return return Content("File deleted successfully"); } else { // File does not exist message incase file not exists on folder return Content("File not found"); } } catch (Exception ex) { // Handle exceptions, log errors, or return an error message from here return Content($"Error deleting file: {ex.Message}"); } }add link to delete in .cshtml
@Html.ActionLink("Delete File", "DeleteFile", "File", new { fileName = "FileNameToDelete.txt" }, null)Naimish MakwanaRank #324 · 7.7K reputationPosted Mar 7, 2024, 2:20 AM EST
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.
using System.IO; using System.Web.Mvc; namespace YourNamespace.Controllers { public class HomeController : Controller { public ActionResult DeleteFile(string fileName) { string filePath = Server.MapPath("~/YourDirectory/" + fileName); if (System.IO.File.Exists(filePath)) { System.IO.File.Delete(filePath); ViewBag.Message = "File deleted successfully."; } else { ViewBag.Message = "File not found."; } return View(); } } }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:@Html.ActionLink("Delete File", "DeleteFile", "Home", new { fileName = "YourFile.extension" }, null)Thanks
Andrew Bhaur4 reputationPosted Mar 6, 2024, 8:26 PM EST
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.
Jayraj ChhayaRank #304 · 8.2K reputationPosted Mar 7, 2024, 4:15 AM EST
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:using System; using System.IO; public class FileOperations { public void DeleteFile(string filePath) { if (File.Exists(filePath)) { File.Delete(filePath); Console.WriteLine("File deleted successfully."); } else { Console.WriteLine("File not found."); } } }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.Emmmanuel FIADUFERank #947 · 2.2K reputationPosted Mar 7, 2024, 6:15 AM EST
Thank you team, it is working now
Rohini ParadeRank #1,014 · 1.9K reputationPosted Mar 7, 2024, 11:42 AM EST
You can use
string directory = @"C:\temp";
string filename = @"readme1.txt";
try {
File.Delete(Path.Combine(directory, filename));
} catch (IOException ex) { WriteLine(ex.Message); }