Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i am uploading image but if some image with same name then image override ...!! so what can i do ??
may i have to create new folder to store this image ? or what other possibly solution ??

if i have to create new folder then how this posible what code i have to write ??

What I have tried:

[HttpPost]
public ActionResult Index(upload upload,HttpPostedFileBase File)
{

if (File == null)
{
ModelState.AddModelError("customeerror", "Please Upload Your file");
}


try
{
// string filename = Guid.NewGuid() + Path.GetExtension(File.FileName);
string filename = Path.GetFileName(File.FileName);
File.SaveAs(Path.Combine(Server.MapPath("~/uploadcv"), filename));
using (NGOEntities1 db = new NGOEntities1())
{
upload.resume = filename;
db.uploads.Add(upload);
db.SaveChanges();
}
ModelState.Clear();
upload = null;
ViewBag.message = "Succcess";


}
catch(Exception Ex)
{
ViewBag.message = "Error";
return View();
}
return RedirectToAction("list");
}
Posted
Updated 18-Feb-16 23:18pm

1 solution

Use File.Exists[^] to see if the file already exists, and if it does alter it like adding a number to the end, then checking again and so on until you file a file that doesn't exist.

Bear in mind people uploading at the same time though, you might want to implement locking around this code.
 
Share this answer
 
Comments
Member 12256508 19-Feb-16 5:34am    
thanks , i search about this solution ...i get same solution which u tell me ..but i am beginner of asp.net mvc. so dont no exactly what i can do ! can u give some link ? or what code i write there so problem can be solve ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900