locked
ASP NET MVC 5 Remove File From Website RRS feed

  • Question

  • User-2075079779 posted

    View Code

      @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>
        }

    Controller

        [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);
    }

    I want to delete the picture with the button. How can I do it ? I can delete products but I can not delete pictures. I can delete products with Id. I tried to do examples on the internet but I could not. Would you make an illustrative example? Sorry for bad english.

    Wednesday, December 20, 2017 9:46 AM

All replies

  • User753101303 posted

    Hi,

    And the exact issue is ? It seems you are storing the path to an image for each product so if using Server.MapPath(p.Image) you should be able to get the path of the file you want to delete (using https://msdn.microsoft.com/en-us/library/system.io.file.delete(v=vs.110).aspx )

    If you tried something that fails it could be easier to tell what failed so that we can better understand what is blocking you from doing that.

    Not directly related by I would not use the client side file name. Currently you may have a risk for overwriting a product image with some other file as you are reusing the client side file name. I often prefer to keep the client file name if needed as part of the db but to use my own naming scheme (a guid or a primary key) to name the file that is saved on the server side.

    Wednesday, December 20, 2017 11:52 AM
  • User-2075079779 posted

    I want to use the following code. But I do not know how to use it 

    string " " = Request.MapPath(" ");
    if (System.IO.File.Exists(" "))
    {
       System.IO.File.Delete(" ");
    }
    Wednesday, December 20, 2017 1:02 PM
  • User-832373396 posted

    Hi <g class="gr_ gr_5 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="5" data-gr-id="5">tomclack</g>,

    tomclack

    I want to use the following code. But I do not know how to use it 

    string " " = Request.MapPath(" ");
    if (System.IO.File.Exists(" "))
    {
       System.IO.File.Delete(" ");
    }

    Sir, please refer to this working example and usage:

    public void Index11() {  
    string folderpath = Request.MapPath("~/foldername"); //2 :or string folderpath = Server.MapPath("~/foldername"); if (Directory.Exists(folderpath)) { Directory.Delete(folderpath, true); } Directory.CreateDirectory(folderpath); string path2 = Request.MapPath("~/foldername/a.html"); //2 or string path2 = Path.Combine(folderpath, "a.html"); //3 or string path2 = Server.MapPath("~/foldername/a.html" System.IO.File.Create(path2);

    With regards, Angelina Jolie

    Thursday, December 21, 2017 2:54 AM