Example #1
0
        public async Task <HttpResponseMessage> ImportAlumni()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            string root     = HttpContext.Current.Server.MapPath("~/" + UPLOAD_ALUMNI_DIR);
            var    provider = new MultipartFormDataStreamProvider(root);

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

                SliderProxy slider = new SliderProxy();
                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_ALUMNI_DIR, fileName);
                    var moveTo = Path.Combine(root, fileName);
                    if (File.Exists(moveTo))
                    {
                        File.Delete(moveTo);
                    }
                    File.Move(file.LocalFileName, moveTo);

                    using (FileStream stream = System.IO.File.Open(moveTo, FileMode.Open, FileAccess.Read))
                    {
                        var importData = new List <AlumniProxy>();
                        using (var reader = ExcelReaderFactory.CreateReader(stream))
                        {
                            var result = reader.AsDataSet();
                            var data   = result.Tables[0];
                            for (var row = 0; row < data.Rows.Count; row++)
                            {
                                if (row == 0)
                                {
                                    continue;
                                }
                                importData.Add(new AlumniProxy
                                {
                                    StudentId     = data.Rows[row].ItemArray[AlumniColumn.StudentID].ToString(),
                                    FullName_TH   = data.Rows[row].ItemArray[AlumniColumn.FullName_TH].ToString(),
                                    FullName_EN   = data.Rows[row].ItemArray[AlumniColumn.FullName_EN].ToString(),
                                    GraduatedYear = data.Rows[row].ItemArray[AlumniColumn.GraduatedYear].ToString(),
                                });
                            }
                        }
                        if (importData.Count() > 0)
                        {
                            this._alumniService.ClearAll(importData.FirstOrDefault().GraduatedYear);
                            foreach (var alumni in importData)
                            {
                                this._alumniService.CreateNew(new Alumni
                                {
                                    StudentId     = alumni.StudentId,
                                    FullName      = alumni.FullName_TH,
                                    FullName_EN   = alumni.FullName_EN,
                                    GraduatedYear = alumni.GraduatedYear
                                });
                            }
                        }
                    }
                }
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }
Example #2
0
        public async Task <HttpResponseMessage> PostFormData()
        {
            if (!Request.Content.IsMimeMultipartContent())
            {
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            var    hasFile  = false;
            var    GuID     = Guid.NewGuid().ToString();
            string root     = HttpContext.Current.Server.MapPath("~/" + UPLOAD_BANNER_DIR);
            var    provider = new MultipartFormDataStreamProvider(root);

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

                SliderProxy slider = new SliderProxy();
                foreach (var key in provider.FormData)
                {
                    if (key.Equals("slider"))
                    {
                        var json = provider.FormData[key.ToString()];
                        slider = JsonConvert.DeserializeObject <SliderProxy>(json);
                    }
                }
                string path = string.Empty;
                foreach (MultipartFileData file in provider.FileData)
                {
                    hasFile = true;
                    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_BANNER_DIR, fileName);
                    var moveTo = Path.Combine(root, fileName);
                    if (File.Exists(moveTo))
                    {
                        File.Delete(moveTo);
                    }
                    File.Move(file.LocalFileName, moveTo);
                }
                var v = new Banner
                {
                    Title_EN       = slider.Title_EN,
                    Title_TH       = slider.Title_TH,
                    ImageUrl       = path,
                    Description_EN = slider.Description_EN,
                    Description_TH = slider.Description_TH,
                    IsActive       = slider.IsActive
                };
                if (slider.Id == 0)
                {
                    this._bannerRepository.Add(v);
                }
                else
                {
                    var existing = this._bannerRepository.FindById(slider.Id);
                    existing.Title_EN       = slider.Title_EN;
                    existing.Title_TH       = slider.Title_TH;
                    existing.Description_EN = slider.Description_EN;
                    existing.Description_TH = slider.Description_TH;
                    existing.IsActive       = slider.IsActive;
                    if (hasFile)
                    {
                        existing.ImageUrl = path;
                    }
                    this._bannerRepository.Update(existing);
                }
                return(Request.CreateResponse(HttpStatusCode.OK));
            }
            catch (System.Exception e)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e));
            }
        }