0

大家好,我正在研究 mvc3

在这里我需要从会话数据中删除以前上传的文件

anh我在插入数据库之前显示文件所以我在会话中显示数据现在我需要删除以前选择的文件请帮助如何获取选定的文件索引值以从会话中删除文件

例如在这里检查这篇文章它是在c#中,但我需要这是在mvc3中请帮助我完成这项工作请帮助我任何人

我的模型在这里

 using System;
  using System.Collections.Generic;
   using System.Linq;
  using System.Web;
   using System.ComponentModel.DataAnnotations;

   namespace BugTracker.Models
 {
public class BugModel
{


    public BugModel()
    {
        if (ListFile == null)
            ListFile = new List<BugAttachment>();
    }
    public List<BugAttachment> ListFile { get; set; }

    public string ErrorMessage { 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 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);
    } 

这里是我的查看页面

    <div class="UploadMain">
    <%:Html.Label("Attachment Name:") %>
    <%:Html.TextBoxFor(model=>model.AttachmentName) %>
    <span>
        <%:Html.Label("Upload Files") %></span>
    <input type="file" name="file" id="file" />
    <input type="submit" value="Upload" id="Upload" class="style-name cancel"  />
    <%--onclick="window.location.href='<%= Url.Action("UploadFile", "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>
</div> 

例如在这里检查这篇文章它是在c#中,但我需要这是在mvc3中请帮助我完成这项工作请帮助我任何人

提前致谢

4

1 回答 1

1

要删除您上传的文件,您只需要它的文件路径,并使用 File.Delete("filepath"); 要知道要删除哪个文件,您的 Delete 操作应采用 id:

删除(int id)

然后在您的操作链接中传递 BugAttachmentID:(使用路由值/参数)

<%:Html.ActionLink("Delete", "Delete", new { @FileName = Emp.FileName }, new { id = Emp.BugAttachmentID })%>

然后在您的删除方法中,您使用 ID 在 FileList 中查找要删除的文件。然后使用附件 url 调用 File.Delete。

我希望这有帮助。

于 2012-09-06T14:33:19.340 回答