小编Tut*_*uta的帖子

删除MVC5中的上传文件?

我正在使用MVC5项目,我创建了一个简单的系统,用户可以为每个Employee上传文件"CV".现在除了"DELETING File"之外,所有的事情对我都很好.我需要添加删除上传文件的操作方法以及用另一个文件替换它的功能.

在模型类中,我创建了两个属性HttpPostedFileBase CV来保存上传的文件和String cvName,以保存文件名并使用它来创建指向该文件的链接.

在控制器中我做了什么:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DeleteCV(string cvName)
{
    //Session["DeleteSuccess"] = "No";
    var CVName = "";
    CVName = cvName;
    string fullPath = Request.MapPath("~/Content/CVs/" + CVName);

    if (System.IO.File.Exists(fullPath))
    {
        System.IO.File.Delete(fullPath);
        //Session["DeleteSuccess"] = "Yes";
    }
    return RedirectToAction("Index");
} 
Run Code Online (Sandbox Code Playgroud)

这是观点:

<div class="form-group">
    @Html.LabelFor(model => model.CV, htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        @{
            if (File.Exists(Server.MapPath("~/Content/CVs/"
             + Html.DisplayFor(model => model.cvName))))
            {
                <a href="~/Content/CVs/@Html.DisplayFor(model => model.cvName)"> @Html.DisplayFor(model => model.cvName)  @Html.HiddenFor(model => model.cvName)</a>
                <a href="@Url.Action("DeleteCV", new { @Model.cvName })"> …
Run Code Online (Sandbox Code Playgroud)

asp.net asp.net-mvc asp.net-mvc-3 asp.net-mvc-4

1
推荐指数
1
解决办法
1433
查看次数