代码之家  ›  专栏  ›  技术社区  ›  tom clack

ASP NET MVC 5从网站删除文件

  •  1
  • tom clack  · 技术社区  · 7 年前

    查看代码

    @using(Html.BeginForm("Edit",
        "Products",
        FormMethod.Post,
        new {
            enctype = "multipart/form-data"
        })) {
        @Html.AntiForgeryToken()
    
        < div class = "form-horizontal" >
    
            @Html.ValidationSummary(true, "", new {
                @class = "text-danger"
            })
        @Html.HiddenFor(model => model.Id)
    
        < div class = "form-group" >
            @Html.LabelFor(model => model.Image, htmlAttributes: new {
                @class = "control-label col-md-2"
            }) < div class = "col-md-10" >
            < img src = "@Url.Content(Model.Image)"
        width = "150" / >
            < /div> < /div>
    }
    

    控制器

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit(Product product, HttpPostedFileBase file) {
     if (ModelState.IsValid) {
      Product p = new Product {
       Id = product.Id,
        Name = product.Name,
        Description = product.Description,
        Image = product.Image
      };
    
      if (file != null) {
       string Image = Path.Combine(Server.MapPath("~/Upload"), Path.GetFileName(file.FileName));
       file.SaveAs(Image);
       p.Image = "~/Upload/" + file.FileName;
      }
    
      db.Entry(p).State = EntityState.Modified;
      db.SaveChanges();
      return RedirectToAction("Index");
     } else {
      return View(product);
     }
    
    }
    
    public ActionResult Edit(int ? id) {
     if (id == null) {
      return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
     }
     Product product = db.Products.Find(id);
     if (product == null) {
      return HttpNotFound();
     }
     return View(product);
    }
    

    我想用按钮删除图片。我该怎么做?我可以删除产品,但不能删除图片。我可以删除Id为的产品。我尝试在互联网上做示例,但我做不到。你能举例说明吗?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Shaiju T    7 年前

    下面是一些基本设置,应该适合您。

    操作方法:

    public ActionResult Index()
    {
    
    //Adding some dummy product data for example, Usually you will get  real details from DB.
    
      List<Product> products = new List<Product>();
    
        Product product1 = new Product()
        {
           Id = 1,
           Name = "Bint Beef",
           Description = "Product Bint Beef",
           ImageName = "bintbeef-1"
        };
    
        Product product2 = new Product()
        {
           Id = 2,
           Name = "Bint Beef 2",
           Description = "Product Bint Beef 2",
           ImageName = "bintbeef-2"
        };
    
         products.Add(product1);
         products.Add(product2);
    
         return View(products);
        }
    
    
    
    [HttpPost]
    public ActionResult DeleteProductImage(int productID)
    {
        try
        {
          string file_name = Convert.ToString(productID);
    
         //Here you can instead use `ImageName` property to fetch the name from db based 
            on product id and then delete image
    
         string path = Server.MapPath("~/Content/Images/" + file_name + ".jpg");
         FileInfo file = new FileInfo(path);
         if (file.Exists)//check file exsit or not
         {
             file.Delete();
         }
    
         return Json(new { status = true }, JsonRequestBehavior.AllowGet);
         }
    
         catch
         {
         return Json(new { status = false }, JsonRequestBehavior.AllowGet);
         }
    
        }
    

    索引视图

    @model IEnumerable<DemoMvc.Models.Product>
    
    <h2>Product List</h2>
    
    
    <table class="table">
        <tr>
    
            <th>
                Product Id
            </th>
            <th>
                Name
            </th>
    
            <th>
                Image Name
            </th>
    
        </tr>
    
        @foreach (var item in Model)
        {
            <tr>
                <th>
                    @item.Id
                </th>
                <td>
                    @item.Name
                </td>
    
                <td>
                    @item.ImageName
                </td>
    
                <td>
    
                 @using (Html.BeginForm("DeleteProductImage", "Home"))
                 {
    
                    <input name="productID" type="hidden" value="@item.Id" />
    
                    <button type="submit">Delete Image</button>
                 }
    
                </td>
            </tr>
        }
    
    </table>
    

    Reference .