Hi all i have a TextBox and a Fileupload controll and a Table to show the uploaded Files…and i have a delete link in my table..so that user can delete any of the uploaded files before clicking on submit button….for this i have model
public BugModel()
{
if (ListFile == null)
ListFile = new List<BugAttachment>();
}
public List<BugAttachment> ListFile { get; set; }
}
public class BugAttachment
{
public string FileName { get; set; }
public int BugAttachmentID { get; set; }
public string AttachmentName { get; set; }
public int BugID { get; set; }
public string AttachmentUrl { get; set; }
public string AttachedBy { get; set; }
public string ErrorMessage { get; set; }
}
when ever user uploads a file i keep them in Listfile list and show them in table..now what i want is i want to delete the uploaded file from server and from the Listfile also
..i had succede deleting the file from uploaded files folder …now i want to remove the AttachmentName and AttachmentUrl from my ListFile also when ever user clicks on delete link..how should i do this..any ideas are much appreciated
this is what i had done till now
public ActionResult Delete(string FileName, BugModel model)
{
if (Session["CaptureData"] == null)
{
}
else
{
model = (BugModel)Session["CaptureData"];
}
char DirSeparator = System.IO.Path.DirectorySeparatorChar;
string FilesPath = ";" + FileName;
string filenameonly = name + Path.GetFileName(FilesPath);
string FPath = "Content" + DirSeparator + "UploadedFiles" + DirSeparator + filenameonly;
// Don't do anything if there is no name
if (FileName.Length == 0) return View();
// Set our full path for deleting
string path = FilesPath + DirSeparator;
// Check if our file exists
if (System.IO.File.Exists(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + FPath)))
{
// Delete our file System.IO.File.Delete(Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + FPath));
}
return View("LoadBug");
}
If the
FileNameis unique, you can create a new List of BugAttachments objects and exclude a particular object with the specified FileNameNow
newBugAttachmentswill have the Items after deletion.You may also use
RemoveAllmethod which updates the original ListAssuming
GetAllAttachmentsFromSomeWheremethod returns a List of AvailableBugAttachmentobjects for the specifiedBugModelandFileNameis the parameter which has the FileName to be deleted