public async Task <IHttpActionResult> PostEventBoard() { PetterResultType <EventBoard> petterResultType = new PetterResultType <EventBoard>(); List <EventBoard> eventBoards = new List <EventBoard>(); List <EventBoardFile> eventBoardFiles = new List <EventBoardFile>(); EventBoard eventBoard = new EventBoard(); if (Request.Content.IsMimeMultipartContent()) { string folder = HostingEnvironment.MapPath(UploadPath.EventBoardPath); Utilities.CreateDirectory(folder); var provider = await Request.Content.ReadAsMultipartAsync(); foreach (var content in provider.Contents) { string fieldName = content.Headers.ContentDisposition.Name.Trim('"'); if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName)) { EventBoardFile eventBoardFile = new Models.EventBoardFile(); var file = await content.ReadAsByteArrayAsync(); string fileName = Utilities.additionFileName(content.Headers.ContentDisposition.FileName.Trim('"')); if (!FileExtension.EventBoardExtensions.Any(x => x.Equals(Path.GetExtension(fileName.ToLower()), StringComparison.OrdinalIgnoreCase))) { petterResultType.IsSuccessful = false; petterResultType.JsonDataSet = null; petterResultType.ErrorMessage = ResultErrorMessage.FileTypeError; return(Ok(petterResultType)); } string fullPath = Path.Combine(folder, fileName); File.WriteAllBytes(fullPath, file); string thumbnamil = Path.GetFileNameWithoutExtension(fileName) + "_thumbnail" + Path.GetExtension(fileName); Utilities.ResizeImage(fullPath, thumbnamil, FileSize.EventBoardWidth, FileSize.EventBoardHeight, ImageFormat.Png); // 이벤트게시판 대표 이미지 if (fieldName == FieldName.EventFieldName) { eventBoard.FileName = fileName; eventBoard.FilePath = UploadPath.EventBoardPath.Replace("~", ""); } eventBoardFile.FileName = fileName; eventBoardFile.FilePath = UploadPath.EventBoardPath.Replace("~", ""); eventBoardFiles.Add(eventBoardFile); } else { string str = await content.ReadAsStringAsync(); string item = HttpUtility.UrlDecode(str); #region switch case switch (fieldName) { case "MemberNo": eventBoard.MemberNo = int.Parse(item); break; case "Title": eventBoard.Title = item; break; case "Content": eventBoard.Content = item; break; default: break; } #endregion switch case } } eventBoard.StateFlag = StateFlags.Use; eventBoard.DateCreated = DateTime.Now; eventBoard.DateModified = DateTime.Now; // 이벤트게시판 등록 db.EventBoards.Add(eventBoard); int num = await this.db.SaveChangesAsync(); // 이벤트게시판 파일 등록 foreach (var item in eventBoardFiles) { item.EventBoardNo = eventBoard.EventBoardNo; } db.EventBoardFiles.AddRange(eventBoardFiles); int num1 = await this.db.SaveChangesAsync(); eventBoards.Add(eventBoard); petterResultType.IsSuccessful = true; petterResultType.JsonDataSet = eventBoards; } else { petterResultType.IsSuccessful = false; petterResultType.JsonDataSet = null; } return(Ok(petterResultType)); }
public async Task<IHttpActionResult> PostEventBoard() { PetterResultType<EventBoard> petterResultType = new PetterResultType<EventBoard>(); List<EventBoard> eventBoards = new List<EventBoard>(); List<EventBoardFile> eventBoardFiles = new List<EventBoardFile>(); EventBoard eventBoard = new EventBoard(); if (Request.Content.IsMimeMultipartContent()) { string folder = HostingEnvironment.MapPath(UploadPath.EventBoardPath); Utilities.CreateDirectory(folder); var provider = await Request.Content.ReadAsMultipartAsync(); foreach (var content in provider.Contents) { string fieldName = content.Headers.ContentDisposition.Name.Trim('"'); if (!string.IsNullOrEmpty(content.Headers.ContentDisposition.FileName)) { EventBoardFile eventBoardFile = new Models.EventBoardFile(); var file = await content.ReadAsByteArrayAsync(); string fileName = Utilities.additionFileName(content.Headers.ContentDisposition.FileName.Trim('"')); if (!FileExtension.EventBoardExtensions.Any(x => x.Equals(Path.GetExtension(fileName.ToLower()), StringComparison.OrdinalIgnoreCase))) { petterResultType.IsSuccessful = false; petterResultType.JsonDataSet = null; petterResultType.ErrorMessage = ResultErrorMessage.FileTypeError; return Ok(petterResultType); } string fullPath = Path.Combine(folder, fileName); File.WriteAllBytes(fullPath, file); string thumbnamil = Path.GetFileNameWithoutExtension(fileName) + "_thumbnail" + Path.GetExtension(fileName); Utilities.ResizeImage(fullPath, thumbnamil, FileSize.EventBoardWidth, FileSize.EventBoardHeight, ImageFormat.Png); // 이벤트게시판 대표 이미지 if (fieldName == FieldName.EventFieldName) { eventBoard.FileName = fileName; eventBoard.FilePath = UploadPath.EventBoardPath.Replace("~", ""); } eventBoardFile.FileName = fileName; eventBoardFile.FilePath = UploadPath.EventBoardPath.Replace("~", ""); eventBoardFiles.Add(eventBoardFile); } else { string str = await content.ReadAsStringAsync(); string item = HttpUtility.UrlDecode(str); #region switch case switch (fieldName) { case "MemberNo": eventBoard.MemberNo = int.Parse(item); break; case "Title": eventBoard.Title = item; break; case "Content": eventBoard.Content = item; break; default: break; } #endregion switch case } } eventBoard.StateFlag = StateFlags.Use; eventBoard.DateCreated = DateTime.Now; eventBoard.DateModified = DateTime.Now; // 이벤트게시판 등록 db.EventBoards.Add(eventBoard); int num = await this.db.SaveChangesAsync(); // 이벤트게시판 파일 등록 foreach (var item in eventBoardFiles) { item.EventBoardNo = eventBoard.EventBoardNo; } db.EventBoardFiles.AddRange(eventBoardFiles); int num1 = await this.db.SaveChangesAsync(); eventBoards.Add(eventBoard); petterResultType.IsSuccessful = true; petterResultType.JsonDataSet = eventBoards; } else { petterResultType.IsSuccessful = false; petterResultType.JsonDataSet = null; } return Ok(petterResultType); }