Search code examples
asp.net-mvc-4kendo-upload

system.io.file.exists not working in mvc4


I am removing the image from kendo upload control.

This is my code

  public ActionResult Remove(string[] fileNames)
    {

        if (fileNames != null)
        {
            foreach (var fullName in fileNames)
            {
                var fileName = Path.GetFileName(fullName);
                var physicalPath = Server.MapPath(Path.Combine(("~/AssetAttachments"),fileName));

                if (System.IO.File.Exists(physicalPath))
                {
                     System.IO.File.Delete(physicalPath);
                }
            }
        }
        return Content("");
    }

Physicalpath i have is E:\karthik related\JPL\Dev\Src\AssetTrackingSystem\AssetTrackingSystem\AssetAttachments\Attach3.jpg

Even though file and directory available

  if (System.IO.File.Exists(physicalPath))

is returning false and coming out of condition.

Your help will be appreciated.


Solution

  • Try this:

    foreach (var fullName in fileNames)
    {
         var physicalPath = System.IO.Path.Combine(HttpContext.Current.Server.MapPath("~/AssetAttachments"), fullName);
    
         if (System.IO.File.Exists(physicalPath))
         {
             System.IO.File.Delete(physicalPath);
         }
    }