예제 #1
0
        public ActionResult Addalbum(Album obj, HttpPostedFileBase file)
        {
            AlbumDAO dao = new AlbumDAO();

            if (file != null && file.ContentLength > 0)
            {
                string fileName = Path.GetFileNameWithoutExtension(file.FileName);
                fileName += "_" + obj.IdAlbum;
                fileName += Path.GetExtension(file.FileName);

                string folderPath  = Server.MapPath("~") + @"\Assets\images\ImagesOutSource\ImagesSong";
                string folderPath2 = Server.MapPath("~") + @"\Assets\images\ImagesOutSource\Album";

                if (!Directory.Exists(folderPath))
                {
                    Directory.CreateDirectory(folderPath);
                }

                string path  = Path.Combine(folderPath, fileName);
                string path2 = Path.Combine(folderPath2, fileName);

                file.SaveAs(path);
                file.SaveAs(path2);
                obj.AnhAlbum = fileName;
                int id = dao.Addalbum(obj);
                if (id != -1)
                {
                    return(RedirectToAction("Index", "Album"));
                }
                else
                {
                    ViewBag.Message("Them moi khong thanh cong");
                    return(RedirectToAction("Addalbum", "Album"));
                }
            }
            else
            {
                ModelState.AddModelError("", "Bạn phải nhập vào trường này");
            }
            NgheSiDAO nsDAO = new NgheSiDAO();
            var       ns    = nsDAO.Listof();
            KhuVucDAO kvDAO = new KhuVucDAO();
            var       kv    = kvDAO.ListOf();

            ViewBag.ns = ns;
            ViewBag.kv = kv;
            return(View());
        }