Beispiel #1
0
        public ActionResult Create(EmployeeDetailModels model)
        {
            try
            {
                if (model.PictureUpload != null && !Commons.ImageExtendFiler.Contains(model.PictureUpload.ContentType))
                {
                    ModelState.AddModelError("PictureUpload", Commons.ErrorMsgFilterImage);
                }

                if (!ModelState.IsValid)
                {
                    if ((ModelState["PictureUpload"]) != null && (ModelState["PictureUpload"]).Errors.Count > 0)
                    {
                        model.ImageURL = "";
                    }
                    model.ListCountry   = _factory.GetListCountry();
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(View(model));
                }

                //=============================
                model.Email = model.Email.ToLower();
                byte[] photoByte = null;
                if (model.PictureUpload != null && model.PictureUpload.ContentLength > 0)
                {
                    Byte[] imgByte = new Byte[model.PictureUpload.ContentLength];
                    model.PictureUpload.InputStream.Read(imgByte, 0, model.PictureUpload.ContentLength);
                    model.PictureByte   = imgByte;
                    model.ImageURL      = Guid.NewGuid() + Path.GetExtension(model.PictureUpload.FileName);
                    model.PictureUpload = null;
                    photoByte           = imgByte;
                }
                else
                {
                    if (!string.IsNullOrEmpty(model.ImageURL))
                    {
                        model.ImageURL = model.ImageURL.Replace(Commons.PublicImages, "").Replace(Commons.Image200_200, "");
                    }
                }
                string msg = "";
                var    tmp = model.PictureByte;
                model.PictureByte = null;
                bool result = _factory.CreateOrEdit(model, ref msg);
                if (result)
                {
                    model.PictureByte = tmp;
                    ////Save Image on Server
                    if (!string.IsNullOrEmpty(model.ImageURL) && model.PictureByte != null)
                    {
                        var          originalDirectory = new DirectoryInfo(string.Format("{0}Uploads\\", Server.MapPath(@"\")));
                        var          path = string.Format("{0}{1}", originalDirectory, model.ImageURL);
                        MemoryStream ms   = new MemoryStream(photoByte, 0, photoByte.Length);
                        ms.Write(photoByte, 0, photoByte.Length);
                        System.Drawing.Image imageTmp = System.Drawing.Image.FromStream(ms, true);
                        ImageHelper.Me.SaveCroppedImage(imageTmp, path, model.ImageURL, ref photoByte);
                        model.PictureByte = photoByte;
                        FTP.Upload(model.ImageURL, model.PictureByte);
                        ImageHelper.Me.TryDeleteImageUpdated(path);
                    }
                    return(RedirectToAction("Index"));
                }
                else
                {
                    //return RedirectToAction("Create");
                    ModelState.AddModelError("Error", msg);
                    model.ImageURL    = "";
                    model.ListCountry = _factory.GetListCountry();
                    return(View(model));
                }
            }
            catch (Exception ex)
            {
                NSLog.Logger.Error("Employee_Create: ", ex);
                return(new HttpStatusCodeResult(400, ex.Message));
            }
        }