public async Task<IHttpActionResult> PutNoticeFile(int id, NoticeFile noticeFile) { if (!ModelState.IsValid) { return BadRequest(ModelState); } if (id != noticeFile.NoticeFileNo) { return BadRequest(); } db.Entry(noticeFile).State = EntityState.Modified; try { await db.SaveChangesAsync(); } catch (DbUpdateConcurrencyException) { if (!NoticeFileExists(id)) { return NotFound(); } else { throw; } } return StatusCode(HttpStatusCode.NoContent); }
public async Task<IHttpActionResult> PostNoticeFile(NoticeFile noticeFile) { if (!ModelState.IsValid) { return BadRequest(ModelState); } db.NoticeFiles.Add(noticeFile); await db.SaveChangesAsync(); return CreatedAtRoute("DefaultApi", new { id = noticeFile.NoticeFileNo }, noticeFile); }
public async Task<IHttpActionResult> PostNotice() { PetterResultType<Notice> petterResultType = new PetterResultType<Notice>(); List<Notice> notices = new List<Notice>(); List<NoticeFile> noticeFiles = new List<NoticeFile>(); Notice notice = new Notice(); if (Request.Content.IsMimeMultipartContent()) { string folder = HostingEnvironment.MapPath(UploadPath.NoticePath); 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)) { NoticeFile noticeFile = new NoticeFile(); var file = await content.ReadAsByteArrayAsync(); string fileName = Utilities.additionFileName(content.Headers.ContentDisposition.FileName.Trim('"')); if (!FileExtension.NoticeExtensions.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.NoticeWidth, FileSize.NoticeHeight, ImageFormat.Png); noticeFile.FileName = fileName; noticeFile.FilePath = UploadPath.NoticePath.Replace("~", ""); noticeFiles.Add(noticeFile); } else { string str = await content.ReadAsStringAsync(); string item = HttpUtility.UrlDecode(str); #region switch case switch (fieldName) { case "MemberNo": notice.MemberNo = int.Parse(item); break; case "Title": notice.Title = item; break; case "Content": notice.Content = item; break; default: break; } #endregion switch case } } notice.StateFlag = StateFlags.Use; notice.DateCreated = DateTime.Now; notice.DateModified = DateTime.Now; // 공지게시판 등록 db.Notices.Add(notice); int num = await this.db.SaveChangesAsync(); // 공지게시판 파일 등록 foreach (var item in noticeFiles) { item.NoticeNo = notice.NoticeNo; } db.NoticeFiles.AddRange(noticeFiles); int num1 = await this.db.SaveChangesAsync(); notices.Add(notice); petterResultType.IsSuccessful = true; petterResultType.JsonDataSet = notices; } else { petterResultType.IsSuccessful = false; petterResultType.JsonDataSet = null; } return Ok(petterResultType); }