编辑图像删除旧图像文件或替换图像

时间:2019-07-18 16:14:26

标签: asp.net-mvc

我正在将图像上传到文件夹,并将详细信息保存在SQL数据库中。一切正常,但是在编辑时,如何删除旧文件,或者干脆用新文件覆盖旧文件?这是添加和编辑的代码:

谢谢, EB

添加:

[HttpPost]
                        public ActionResult AddDocument(Document newDocument, HttpPostedFileBase file)
                        {
                            string fileExtension = System.IO.Path.GetExtension(file.FileName);
                            string fileName = System.IO.Path.GetFileName(file.FileName);
                            string filePath = "/Documents/" + newDocument.Department + "/" + fileName;
                            file.SaveAs(Server.MapPath(filePath));

                            db.Documents.Add(new Document
                            {
                                DocumentName = fileName,
                                Document_url = filePath,
                                Department = newDocument.Department,
                                DocumentType = fileExtension,
                                Description = newDocument.Description
                            }
                                );
                            db.SaveChanges();
                            return RedirectToAction("Index");
                        }

编辑:

                         [HttpPost]
                                public ActionResult EditDocument(int id, Document document)
                                {
                                    try
                                    {
                                        db.Entry(document).State = System.Data.Entity.EntityState.Modified;
                                        db.SaveChanges();
                                        return RedirectToAction("DocumentDetails", new { id = id });
                                    }
                                    catch
                                    {
                                        return View();
                                    }
                                }


                         [HttpPost]
                                public ActionResult EditDocument(int id, Document document)
                                {
                                    try
                                    {
                                        db.Entry(document).State = System.Data.Entity.EntityState.Modified;
                                        db.SaveChanges();
                                        return RedirectToAction("DocumentDetails", new { id = id });
                                    }
                                    catch
                                    {
                                        return View();
                                    }
                                }

1 个答案:

答案 0 :(得分:0)

这是一种实现方式。

public ActionResult EditDocument(int id, Document document, HttpPostedFileBase file)
{            
    //NOTE that you only need one of the parameters id or document, my example is using document
    try
    {
        string fileExtension = System.IO.Path.GetExtension(file.FileName);
        string fileName = System.IO.Path.GetFileName(file.FileName);
        string filePath = "/Docs/" + document.Department + "/" + fileName;
        document.DocumentType = fileExtension;
        document.Document_url= filePath;
        document.DocumentName = fileName;

        var oldDocument = db.Documents.First(m => m.Id == document.Id);
        //or var oldDocument = db.Documents.First(m => m.Id == id); //if that is how you get thte id
            //you can delete the file here or leave it never referenced in the db
            FileInfo myfileinf = new FileInfo(Server.MapPath(oldDocument.Document_url));
        //check if file exists before deleting
        if (System.IO.File.Exists(myfileinf.FullName))
        {
            System.IO.File.Delete(myfileinf.FullName);
        }
        file.SaveAs(Server.MapPath(filePath));
        //set the old document here
        db.Entry(oldDocument).CurrentValues.SetValues(document);
        db.Entry(oldDocument).State = System.Data.Entity.EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("EditDocument", new { id = id });
    }
    catch(Exception e)
    {
        return View();
    }
}

您还可以查看我的答案here,看看是否有帮助