大家好我有一个TextBox和一个Fileupload控件和一个表来显示上传的文件...我在我的表中有一个删除链接..所以用户可以在点击提交按钮之前删除任何上传的文件....为此我有模特
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; }
}
当用户上传文件时我将它们保存在Listfile列表中并在表格中显示..现在我想要的是我想要从服务器和Listfile中删除上传的文件 ..i成功从上传的文件夹中删除文件...现在我想从用户点击删除链接时从我的ListFile中删除AttachmentName和AttachmentUrl ..我应该怎么做...非常感谢任何想法< / p>
这是我到目前为止所做的事情
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");
}
答案 0 :(得分:1)
如果FileName
是唯一的,您可以创建一个新的BugAttachments对象列表,并使用指定的FileName排除特定对象
List<BugAttachment> allBugAttachemnts=GetAllAttachmentsFromSomeWhere();
List<BugAttachment> newBugAttachments = allBugAttachemnts.ListFile.
Where(x => x.FileName!= FileName).ToList();
现在newBugAttachments
将删除后的项目。
您也可以使用更新原始列表的RemoveAll
方法
allBugAttachemnts.RemoveAll(x => x.FileName!= FileName);
假设GetAllAttachmentsFromSomeWhere
方法返回指定BugAttachment
的可用BugModel
对象列表,FileName
是具有要删除的FileName的参数