Asp.net mvc 3 如何从服务器上删除上载的文件?

Asp.net mvc 3 如何从服务器上删除上载的文件?,asp.net-mvc-3,file-upload,Asp.net Mvc 3,File Upload,大家好,我有一个上传功能,用户可以上传文件,在插入数据库之前,我会在表中显示上传的文件,如果用户想要删除任何上传的文件,我会在表中提供一个删除选项。我正在尝试这段代码,但文件不会被删除 这是我的文件上传代码 public ActionResult UploadFile(string AttachmentName, BugModel model) { BugModel bug = null; if (Session["CaptureData"] ==

大家好,我有一个上传功能,用户可以上传文件,在插入数据库之前,我会在表中显示上传的文件,如果用户想要删除任何上传的文件,我会在表中提供一个删除选项。我正在尝试这段代码,但文件不会被删除

这是我的文件上传代码

 public ActionResult UploadFile(string AttachmentName, BugModel model)
   {           
    BugModel bug = null;
    if (Session["CaptureData"] == null)
    {
        bug = model;
    }
    else
    {
        bug = (BugModel)Session["CaptureData"];
    }
    foreach (string inputTagName in Request.Files)
    {
        HttpPostedFileBase file1 = Request.Files[inputTagName];
        if (file1.ContentLength > 0)
        {
            BugAttachment attachment = new BugAttachment();
            var allowedExtensions = new[] { ".doc", ".xlsx", ".txt", ".jpeg", ".docx" };
            var extension = Path.GetExtension(file1.FileName);
            if (!allowedExtensions.Contains(extension))
            {
                model.ErrorMessage = "{ .doc, .xlsx, .txt, .jpeg }, files are allowed.... ";
            }
            else
            {
                string filename = Guid.NewGuid() + Path.GetFileName(file1.FileName);
                string path = "/Content/UploadedFiles/" + filename;
                string savedFileName = Path.Combine(Server.MapPath("~" + path));
                file1.SaveAs(savedFileName);
                attachment.FileName = "~" + path.ToString();
                attachment.AttachmentName = AttachmentName;
                attachment.AttachmentUrl = attachment.FileName;
                bug.ListFile.Add(attachment);
                model = bug;
            }
            Session["CaptureData"] = model;
        }
    }
    ModelState.Clear();
    return View("LoadBug", bug);
}
这是我在表格中显示上传文件的视图

 <table align="center" class="gridtable" border="0" cellspacing="0" cellpadding="0">
    <tr>
        <th>
            Attachment Name
        </th>
        <th>
            Attachment Url
        </th>
        <th>
        Action 
        </th>
    </tr>
    <% if (Model != null && Model.ListFile != null)
       {  %>
    <% foreach (var Emp in Model.ListFile)
       { %>
    <tr class="Data">
        <td >
            <%:Emp.AttachmentName %>
        </td>
        <td >
            <%: Emp.FileName %>
        </td>
       <td>
      <%-- <%= Html.ActionLink("Delete", "Delete")%>--%>

        <%:Html.ActionLink("Delete", "Delete", new { @FileName = Emp.FileName })%>
        </td>
    </tr>
    <% } %>
    <% } %>
</table>

文件路径(即全名)是否正确?看起来您正在操作后的视图中查看它。IIS_USR是否具有删除该目录中文件的正确权限?是路径显示正确它没有删除文件它会引发此错误视图“eac6a6ee-7a54-407d-b1e0-25048aa653a6Bug.xlsx”或其主视图未找到,或者没有视图引擎支持搜索的位置。搜索了以下位置:~/Views/Bug/eac6ee-7a54-407d-b1e0-25048aa653a6Bug.xlsx.aspx~/Views/Bug/eac6ee-7a54-407d-b1e0-25048aa653a6Bug.xlsx.ascx~/Views/Shared/eac6ee-7a54-407d-b1e0-xlsx-25048aa653a6Bug.xlsx~/Views/Shared/eac6ee-7a54-407d-b1e0-25048aa656bug.xlsx.aspx~/Shared/eac6ee-7a54-407d-25048a4d-b1e0-25048a6bug.xlsx。还有一个问题未设置fileshare属性该错误与删除文件无关。它失败,因为它找不到与该操作关联的视图。你应该把这个错误放在你的问题里。
  public ActionResult Delete(string FileName)
    {

        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", Bug);
    }
    public string name { get; set; }
    public object Bug { get; set; }
}