コード例 #1
0
ファイル: ForumController.cs プロジェクト: k12club/swu
 public void AddOrUpdatePost(WebboardItemProxy forum)
 {
     if (string.IsNullOrWhiteSpace(forum.Id))
     {
         this._forumService.CreateNewPost(new Forum
         {
             Id               = Guid.NewGuid().ToString(),
             Name             = forum.Name,
             ShortDescription = forum.ShortDescription,
             FullDescription  = forum.FullDescription,
             CategoryId       = forum.CategoryId,
             CreatedDate      = this._datetimeRepository.Now()
         }, forum.UserId);
     }
     else
     {
         this._forumService.UpdatePost(new Forum
         {
             Id               = forum.Id,
             Name             = forum.Name,
             ShortDescription = forum.ShortDescription,
             FullDescription  = forum.FullDescription,
             CategoryId       = forum.CategoryId,
             UpdatedDate      = this._datetimeRepository.Now()
         }, forum.UserId);
     }
 }
コード例 #2
0
ファイル: ResearchController.cs プロジェクト: k12club/swu
        public async Task <HttpResponseMessage> PostFormData()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            var    GuID     = Guid.NewGuid().ToString();
            var    hasFile  = false;
            string root     = HttpContext.Current.Server.MapPath("~/" + UPLOAD_DIR);
            var    provider = new MultipartFormDataStreamProvider(root);

            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                WebboardItemProxy research = new WebboardItemProxy();
                foreach (var key in provider.FormData)
                {
                    if (key.Equals("research"))
                    {
                        var json = provider.FormData[key.ToString()];
                        research = JsonConvert.DeserializeObject <WebboardItemProxy>(json);
                    }
                }
                string path = string.Empty;
                foreach (MultipartFileData file in provider.FileData)
                {
                    string fileName = file.Headers.ContentDisposition.FileName;
                    if (fileName.StartsWith("\"") && fileName.EndsWith("\""))
                    {
                        fileName = fileName.Trim('"');
                    }
                    if (fileName.Contains(@"/") || fileName.Contains(@"\"))
                    {
                        fileName = Path.GetFileName(fileName);
                    }
                    path = string.Format("{0}{1}", UPLOAD_DIR, fileName);
                    var moveTo = Path.Combine(root, fileName);
                    if (File.Exists(moveTo))
                    {
                        File.Delete(moveTo);
                    }
                    File.Move(file.LocalFileName, moveTo);
                    hasFile = true;
                }
                var r = new Research
                {
                    ShortDescription = research.ShortDescription,
                    FullDescription  = research.FullDescription,
                    CategoryId       = research.CategoryId,
                    CreatorName      = research.MoreDetail.CreatorName,
                    Contributor      = research.MoreDetail.Contributor,
                    Name_EN          = research.Name,
                    Name_TH          = research.Name,
                    PublishDate      = research.MoreDetail.PublishDate,
                    Publisher        = research.MoreDetail.Publisher,
                    CreatedDate      = this._datetimeRepository.Now()
                };
                if (string.IsNullOrEmpty(research.Id))
                {
                    r.Id          = GuID;
                    r.AttachFiles = new List <AttachFile> {
                        new AttachFile {
                            FilePath = path
                        }
                    };
                    r.UpdatedDate = this._datetimeRepository.Now();
                    this._researchService.CreateNewResearch(r, research.UserId);
                }
                else
                {
                    r.Id = research.Id;
                    if (hasFile)
                    {
                        r.AttachFiles = new List <AttachFile> {
                            new AttachFile {
                                FilePath = path
                            }
                        };
                    }
                    this._researchService.UpdateResearch(r, research.UserId, hasFile);
                }
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }