public static string SavePhoto(ControllerContext controllerContext, HttpPostedFileBase image)
        {
            var    photosPath = UserContentPathResolver.Resolve(controllerContext, ApplicationFolders.UserPhotoPath);
            string fullPath   = null;

            while (File.Exists(fullPath = Path.Combine(photosPath, Path.GetRandomFileName())))
            {
                ;                                                                               //loop while getting unique file name
            }
            fullPath = fullPath + "." + Path.GetExtension(image.FileName);
            image.SaveAs(fullPath);

            return(fullPath);
        }
Пример #2
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase    request  = controllerContext.HttpContext.Request;
            var                name     = request.Form.Get("newUserName");
            var                id       = request.Form.Get("userId");
            HttpPostedFileBase image    = request.Files.Get("image");
            string             fileName = "";

            if (image != null)
            {
                var fullPath = UserContentPathResolver.SavePhoto(controllerContext, image);
                fileName = Path.GetFileName(fullPath);
            }

            return(new UserProfileModel()
            {
                Id = int.Parse(id),
                Photo = fileName,
                Name = name
            });
        }