Example #1
0
        public ActionResult SetPhoto(SetPhotoModel model)
        {
            if (ModelState.IsValid)
            {
                var result = UploadPhoto(model.PhotoFile);
                if (result)
                {
                    var photo = new Photo {
                        url = Url.Content("/Images/Photos/" + model.PhotoFile.FileName)
                    };

                    if (!CurrentUserInfo.photo_id.HasValue)
                    {
                        _IPhotosRepository.AddPhoto(photo);

                        _IUserInformationsRepository.EditUserInformation(new UserInformation
                        {
                            id            = CurrentUserInfo.id,
                            address_id    = CurrentUserInfo.address_id,
                            first_name    = CurrentUserInfo.first_name,
                            last_name     = CurrentUserInfo.last_name,
                            gender        = CurrentUserInfo.gender,
                            photo_id      = photo.id,
                            register_date = CurrentUserInfo.register_date
                        });
                    }
                    else
                    {
                        var oldPhotoPath = _IPhotosRepository.Photos.FirstOrDefault(x => x.id == CurrentUserInfo.photo_id.Value).url;
                        var fullPath     = Server.MapPath(oldPhotoPath);
                        DeletePhoto(fullPath);

                        photo.id = CurrentUserInfo.photo_id.Value;
                        _IPhotosRepository.ChangePhoto(photo);
                    }
                }
            }
            else
            {
                TempData["Warning"] = "Zdjęcie musi mieć rozszerzenie .jpg, .jpeg lub .png i rozmiar niewiększy niż 3MB.";
            }

            return(RedirectToAction("SetPhoto"));
        }
        public async Task <IActionResult> Upload(Photo photo, IFormFile thePicture)
        {
            //Validation does not check out:
            if (!ModelState.IsValid)
            {
                //  send the user back to the Update
                //  and show the errors
                return(View(photo));
            }
            //validation checks out:
            //  send the photo to the repository
            //  send the user to the AllPhotos action
            photo.DateUploaded = DateTime.Now;

            using MemoryStream memoryStream = new MemoryStream();
            await thePicture.CopyToAsync(memoryStream);

            photo.Picture     = memoryStream.ToArray();
            photo.ContentType = thePicture.ContentType;

            repository.AddPhoto(photo);
            return(RedirectToAction(nameof(AllPhotos)));
        }
Example #3
0
        public async Task <IActionResult> OnPostAsync()
        {
            //Validation does not check out:
            if (!ModelState.IsValid)
            {
                //  send the user back to the Update
                //  and show the errors
                return(Page());
            }
            //validation checks out:
            //  send the photo to the repository
            //  send the user to the AllPhotos action
            Photo.DateUploaded = DateTime.Now;

            using MemoryStream memoryStream = new MemoryStream();
            await ThePicture.CopyToAsync(memoryStream);

            Photo.Picture     = memoryStream.ToArray();
            Photo.ContentType = ThePicture.ContentType;


            repository.AddPhoto(Photo);
            return(RedirectToPage("AllPhotos"));
        }