예제 #1
0
        public ActionResult UploadInformation(HttpPostedFileBase file)
        {
            string path = null;

            try
            {
                if (file == null)
                {
                    return(Json(new { exists = false }));
                }
                string fileName = HttpContext.Server.MapPath("~/Resources/Temp/")
                                  + DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss") + System.IO.Path.GetFileName(file.FileName);
                file.SaveAs(fileName);
                if (!System.IO.File.Exists(fileName))
                {
                    return(Json(new { exists = false }));
                }
                path = fileName;
                ExcelManager excel = new ExcelManager(fileName);
                if (!excel.CheackInformationTemplates())
                {
                    return(Json(new { exists = true, legal = false }));
                }
                SQLManager    manager      = new SQLManager();
                var           informations = excel.GetInformation();
                List <string> majorNames   = new List <string>();
                foreach (var item in informations)
                {
                    majorNames.Add(item.Major);
                }
                if (!manager.CheckMajorExist(majorNames.ToArray()))
                {
                    return(Json(new { exists = true, legal = false }));
                }
                bool flag = manager.AddStudents(informations);
                if (flag)
                {
                    return(Json(new { exists = true, legal = true, success = true }));
                }
                return(Json(new { exists = true, legal = true, success = false }));
            }
            finally
            {
                if (path != null)
                {
                    System.IO.File.Delete(path);
                }
            }
        }