Example #1
0
        // 儲存修改的章節內容
        public ActionResult SaveEditChapters(EditBookModel eb)
        {
            EditBookFactory factory = new EditBookFactory();

            if (Request.Form["bc_id"] != null)
            {
                // 儲存章節標題
                eb.bc_id      = Convert.ToInt32(Request.Form["bc_id"]);
                eb.bc_Content = Request.Form["bc_Content"];

                factory.SaveEditBookChapters(eb);

                // 刪除這個章節的所有檔案 (與資料庫)
                if (eb.Files.Count() > 0)
                {
                    eb.b_id        = Request.Form["bId"];
                    eb.b_Type      = Request.Form["bType"];
                    eb.bc_Chapters = Convert.ToInt32(Request.Form["bcChapters"]);

                    List <string> 該章節所有檔名 = factory.回傳所有該章節的檔名(eb);

                    string 刪除舊檔案路徑 = "../書籍素材/" + eb.b_Type + "素材/" + eb.b_id + "/" + eb.b_id + "-" + eb.bc_Chapters + "/";

                    bool 刪除結果 = false;

                    foreach (string p in 該章節所有檔名)
                    {
                        if (System.IO.File.Exists(Server.MapPath(刪除舊檔案路徑 + p)))
                        {
                            try
                            {
                                // 刪除舊檔案
                                System.IO.File.Delete(Server.MapPath(刪除舊檔案路徑 + p));
                                刪除結果 = true;
                            }
                            catch
                            {
                                // 刪除失敗
                            }
                        }
                    }

                    if (刪除結果 == true)
                    {
                        factory.刪除該章節在資料庫裡的所有的檔名(eb.bc_id);

                        if (eb.b_id != null && eb.b_Type != null && eb.bc_Chapters > 0)
                        {
                            // 儲存新檔案到實體路徑
                            eb.FilesName = new List <string>();

                            int i = 1;
                            foreach (HttpPostedFileBase uploadFile in eb.Files)
                            {
                                if (uploadFile.ContentLength > 0)
                                {
                                    // 這個陣列用來 將多筆章節檔名 儲存到資料庫使用
                                    eb.FilesName.Add(eb.b_id + "-" + eb.bc_Chapters + "-" + i + factory.回傳書籍章節檔案副檔名(uploadFile));

                                    // 上傳時自動編名
                                    uploadFile.SaveAs(Server.MapPath("../書籍素材/" + eb.b_Type + "素材/" + eb.b_id + "/" + eb.b_id + "-" + eb.bc_Chapters + "/" + eb.b_id + "-" + eb.bc_Chapters + "-" + i + factory.回傳書籍章節檔案副檔名(uploadFile)));
                                }
                                i++;
                            }

                            // 儲存新章節檔名進資料庫
                            factory.儲存該章節所有檔名到資料庫(eb);
                        }
                    }
                }
            }

            return(RedirectToAction("EditChaptersList"));
        }