Below is a complete C# example showing how to save an uploaded image to a folder using FormCollection
in an ASP.NET MVC controller. This includes:
-
Creating the directory if it doesn't exist
-
Checking if a file with the same name already exists
-
Deleting the old file if found
-
Saving the new file
đ» C# Code (Controller)
đ Notes
-
~/UploadedImages
is the folder where images will be stored inside your project directory. -
Make sure the folder you're writing to has write permissions, especially on a production server.
-
This code works with standard form submissions, not AJAX. If you're using AJAX or APIs, the logic will be slightly different.