Пример #1
0
        public ActionResult UploadProfilePicture(ClientProfilePictureViewModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (model.ProfilePicture.ContentType == "image/jpeg")                                                                           // If file is .jpg
                    {
                        model.ProfilePicture.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".jpg")); // Save file

                        if (System.IO.File.Exists(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".png")))
                        {
                            System.IO.File.Delete(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".png")); // Delete other files if present
                        }

                        if (System.IO.File.Exists(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".gif")))
                        {
                            System.IO.File.Delete(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".gif")); // Delete other files if present
                        }

                        return(RedirectToAction("Index", "Profile", new { Area = "Customer", message = "Profile picture has been successfully updated." }));
                    }
                    else if (model.ProfilePicture.ContentType == "image/gif")                                                                       // If file is .gif
                    {
                        model.ProfilePicture.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".gif")); // Save file

                        if (System.IO.File.Exists(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".jpg")))
                        {
                            System.IO.File.Delete(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".jpg")); // Delete other files if present
                        }

                        if (System.IO.File.Exists(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".png")))
                        {
                            System.IO.File.Delete(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".png")); // Delete other files if present
                        }

                        return(RedirectToAction("Index", "Profile", new { Area = "Customer", message = "Profile picture has been successfully updated." }));
                    }
                    else if (model.ProfilePicture.ContentType == "image/png")                                                                       // If file is .png
                    {
                        model.ProfilePicture.SaveAs(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".png")); // Save file

                        if (System.IO.File.Exists(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".jpg")))
                        {
                            System.IO.File.Delete(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".jpg")); // Delete other files if present
                        }

                        if (System.IO.File.Exists(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".gif")))
                        {
                            System.IO.File.Delete(System.IO.Path.Combine(Server.MapPath("~/Content/ProfilePictures/"), model.UserName + ".gif")); // Delete other files if present
                        }

                        return(RedirectToAction("Index", "Profile", new { Area = "Customer", message = "Profile picture has been successfully updated." }));
                    }
                    else
                    {
                        return(RedirectToAction("Index", "Profile", new { Area = "Customer", message = "Unsupported image type. Please try again using supported image type." }));
                    }
                }
                catch (UnauthorizedAccessException ex) // IMPORTANT - This exception will probably be thrown on the server, unless we change ProfilePictures folder's permissions right away
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    return(RedirectToAction("Index", "Profile", new { Area = "Customer", message = "File could NOT be saved due to lack of permissions. Please review exception log for more details." }));
                }
                catch (System.IO.IOException ex)
                {
                    // Log exception
                    ErrorHandlingUtilities.LogException(ErrorHandlingUtilities.GetExceptionDetails(ex));

                    return(RedirectToAction("Index", "Profile", new { Area = "Customer", message = "An exception has occured. Please review exception log for more details." }));
                }
            }

            return(RedirectToAction("Index", "Profile", new { Area = "Customer", message = "An error occured while attmpting to upload a profile picture. Profile picture has NOT been updated." }));
        }
Пример #2
0
        public PartialViewResult UploadProfilePicture()
        {
            var clientProfilePictureViewModel = new ClientProfilePictureViewModel(User.Identity.Name);

            return(PartialView("_UploadProfilePicture", clientProfilePictureViewModel));
        }