Exemplo n.º 1
0
        public ActionResult EditSave(AuthorModel a)
        {
            AuthorFactory factory = new AuthorFactory();

            a.a_Birth = Request.Form["a_Birth"];

            if (a.image != null)
            {
                string deleteresult = factory.傳回原照片檔名(a.a_id);

                if (System.IO.File.Exists(Server.MapPath(deleteresult)))
                {
                    try
                    {
                        System.IO.File.Delete(Server.MapPath(deleteresult));
                    }
                    catch
                    {
                        deleteresult = "修改失敗";
                    }
                }

                if (deleteresult != "修改失敗")
                {
                    int point = a.image.FileName.LastIndexOf(".");
                    //副檔名
                    string extention = a.image.FileName.Substring(point, a.image.FileName.Length - point);
                    //這一行會幫我們在上傳到資料庫的時候 重新命名,例如 FB 的檔名那樣,16進制的編碼
                    string photoName = Guid.NewGuid().ToString() + extention;
                    //把 SaveAs 變成相對路徑的方法
                    a.image.SaveAs(Server.MapPath("../作者照片/" + photoName));
                    a.a_Image = "../作者照片/" + photoName;
                }
            }

            factory.Edit(a);

            return(RedirectToAction("List"));
        }