Question

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.

Was it helpful?

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);
     }
}

OTHER TIPS

Try with this,

FileInfo fi = new FileInfo(Path.Combine(("~/AssetAttachments"),fileName));
 if (fi.Exists)
 {
   fi.Delete();
 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top