Exemple #1
0
        public ActionResult Save(string t, string l, string h, string w, string fileName, string key)
        {
            try
            {
                // Get file from temporary folder
                var fn = Path.Combine(Server.MapPath("~/Temp"), Path.GetFileName(fileName));

                // Calculate dimesnions
                int top    = Convert.ToInt32(t.Replace("-", "").Replace("px", ""));
                int left   = Convert.ToInt32(l.Replace("-", "").Replace("px", ""));
                int height = Convert.ToInt32(h.Replace("-", "").Replace("px", ""));
                int width  = Convert.ToInt32(w.Replace("-", "").Replace("px", ""));

                // Get image and resize it, ...
                var img = new WebImage(fn);
                img.Resize(width, height);
                // ... crop the part the user selected, ...
                img.Crop(top, left, img.Height - top - _avatarHeight, img.Width - left - _avatarWidth);
                // ... delete the temporary file,...
                System.IO.File.Delete(fn);
                // ... and save the new one.

                string oldName = new UsuarioViewModel().Avatar();
                if (oldName.Substring(0, 4) != "http")
                {
                    System.IO.File.Delete(Server.MapPath(oldName));
                }

                string newName = String.Format("{0}" + new FileInfo(fn).Extension, key);
                //AssociadoViewModel value = (AssociadoViewModel)getModel().getObject(new AssociadoViewModel() { associadoId = int.Parse(key) });
                //if (value.avatar != null)
                //    newName = value.avatar;

                //string newFileName = System.Configuration.ConfigurationManager.AppSettings["Avatar"] + "/" + newName; // Path.GetFileName(fn);
                string newFileLocation = HttpContext.Server.MapPath(new UsuarioViewModel().Path());
                if (Directory.Exists(Path.GetDirectoryName(newFileLocation)) == false)
                {
                    Directory.CreateDirectory(Path.GetDirectoryName(newFileLocation));
                }

                img.FileName = Path.Combine(newFileLocation, Path.GetFileName(newName));  //newName;
                img.Save(img.FileName);
                return(Json(new { success = true, avatarFileLocation = newFileLocation }));
            }
            catch (Exception ex)
            {
                return(Json(new { success = false, errorMessage = "Não foi possível fazer o upload do arquivo.\nERRORINFO: " + ex.Message }));
            }
        }