/// <summary>
        /// Stores the image to blob and saves name to database.
        /// </summary>
        /// <param name="httpPostedFile">The HTTP posted file.</param>
        /// <param name="memberId">The member identifier.</param>
        /// <param name="userId">The user identifier.</param>
        public void StoreImage(HttpPostedFileBase httpPostedFile, int?memberId = null, string userId = null)
        {
            if (httpPostedFile == null || httpPostedFile.ContentLength == 0)
            {
                return;
            }

            string newFileName = BlobService.GetNewFileName(httpPostedFile.FileName);

            // Save image to blob and save name to database.
            if (memberId != null)
            {
                // If member already has a photo, remove it from blob
                var member = unitOfWork.MemberRepository.FindMemberById(memberId);
                if (!string.IsNullOrEmpty(member.PhotoFileName))
                {
                    blobRepo.DeleteBlob(member.PhotoFileName);
                }

                member.PhotoFileName = newFileName;
                unitOfWork.MemberRepository.Update(member);
                blobRepo.UploadBlob(httpPostedFile, newFileName);
            }
            else if (!string.IsNullOrEmpty(userId)) // App user photo
            {
                // If app user already has a photo, remove it from blob
                var user = unitOfWork.AppUserRepository.FindUserById(userId);
                if (!string.IsNullOrEmpty(user.PhotoFileName))
                {
                    blobRepo.DeleteBlob(user.PhotoFileName);
                }

                user.PhotoFileName = newFileName;
                unitOfWork.AppUserRepository.Update(user);
                blobRepo.UploadBlob(httpPostedFile, newFileName);
            }
        }
Пример #2
0
        public ActionResult UploadBlob(HttpPostedFileBase uploadFile)
        {
            if (uploadFile == null)
            {
                return(View());
            }
            string newFileName = BlobService.GetNewFileName(uploadFile.FileName);

            uploadFile = Request.Files[0];


            bool hasUploaded = repo.UploadBlob(uploadFile, newFileName);

            if (hasUploaded)
            {
                return(RedirectToAction("Index"));
            }
            return(View());
        }