Exemplo n.º 1
0
        public async Task <HttpResponseMessage> PostFormData()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            var    GuID     = Guid.NewGuid().ToString();
            string root     = HttpContext.Current.Server.MapPath("~/" + UPLOAD_DIR);
            var    provider = new MultipartFormDataStreamProvider(root);

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

                CourseDetailProxy course = new CourseDetailProxy();
                foreach (var key in provider.FormData)
                {
                    if (key.Equals("course"))
                    {
                        var json = provider.FormData[key.ToString()];
                        course = JsonConvert.DeserializeObject <CourseDetailProxy>(json);
                    }
                }
                string _newFileName = string.Empty;
                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);
                    }
                    _path = string.Format("{0}{1}", UPLOAD_DIR, fileName);
                    File.Move(file.LocalFileName, moveTo);
                    course.ImageUrl = string.Format("{0}?{1}", _path, this._datetimeRepository.Now());
                }
                if (string.IsNullOrEmpty(course.Id))
                {
                    course.Id          = GuID;
                    course.CreatedDate = this._datetimeRepository.Now();
                    this._courseService.Add(course.ToEntity(), course.CreatedUserId);
                }
                else
                {
                    var c = this._courseRepository.FindById(course.Id);
                    c.ImageUrl        = course.ImageUrl;
                    c.Language        = course.Language;
                    c.Name_EN         = course.Name_EN;
                    c.Name_TH         = course.Name_TH;
                    c.Price           = course.Price;
                    c.FullDescription = course.FullDescription;
                    c.BigImageUrl     = course.BigImageUrl;
                    c.CategoryId      = course.CategoryId;
                    c.UpdatedDate     = this._datetimeRepository.Now();
                    c.UpdatedUser     = course.CreatedUserId;
                    this._courseService.Update(c);
                }
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }