Example #1
0
        private void OnImageUpload(object sender, UploadProfilePicEventArgs e)
        {
            int    fileLength = e.ContentLength;
            string fileName   = "avatar" + Path.GetExtension(e.FileName);
            var    folderName = e.UploaderId;

            byte[] photoBytes = new byte[fileLength];
            e.InputStream.Read(photoBytes, 0, fileLength);

            // saving image
            try
            {
                var processedImg = this.imageProcessorService.ProcessImage(
                    photoBytes,
                    Constants.ThumbnailImageSize,
                    Constants.ThumbnailImageSize,
                    Path.GetExtension(fileName),
                    Constants.ThumbnailImageQualityPercentage);

                var dirToSaveIn = Path.Combine(Server.MapPath("../" + Constants.ContentUploadedProfilesRelPath), folderName);

                this.fileSaverService.SaveFile(processedImg, dirToSaveIn, fileName, true);
            }
            catch (Exception ex)
            {
                this.View.Model.ErrorMessage = ex.Message;
                this.View.Model.Succeeded    = false;
                return;
            }

            // saving image url to db
            try
            {
                var imgUrl   = Constants.ProgrammersSpotUrl + Constants.ContentUploadedProfilesRelPath + folderName + "/" + fileName;
                var uploader = this.userService.GetRegularUserById(e.UploaderId);
                if (e.UserRole == "User")
                {
                    this.userService.UpdateRegularUserAvatarUrl(e.UploaderId, imgUrl);
                }
                else if (e.UserRole == "Firm")
                {
                    this.firmService.UpdateFirmUserAvatarUrl(e.UploaderId, imgUrl);
                }
                else
                {
                    this.View.Model.Succeeded    = false;
                    this.View.Model.ErrorMessage = "Unable to find user.";
                    return;
                }

                this.View.Model.Succeeded = true;
            }
            catch (SqlException ex)
            {
                this.View.Model.ErrorMessage = ex.Message;
                this.View.Model.Succeeded    = false;
            }
        }
Example #2
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.User.Identity.IsAuthenticated)
            {
                Response.Redirect(this.ResolveUrl(string.Format("~/Account/Login?ReturnUrl={0}", HttpUtility.UrlEncode("/Account/Manage"))));
            }

            if (this.Request.Files.Count == 0)
            {
                Response.StatusCode = 400;
                Response.End();
                return;
            }

            var userRole = "";

            if (this.User.IsInRole("User"))
            {
                userRole = "User";
            }
            else if (this.User.IsInRole("Firm"))
            {
                userRole = "Firm";
            }
            else
            {
                Response.StatusCode = 400;
                Response.End();
                return;
            }

            var file = this.Request.Files[0];
            var args = new UploadProfilePicEventArgs()
            {
                ContentLength = file.ContentLength,
                FileName      = file.FileName,
                InputStream   = file.InputStream,
                UserRole      = userRole,
                UploaderId    = this.User.Identity.GetUserId()
            };

            this.EventImageUpload?.Invoke(sender, args);

            Response.ClearContent();
            Response.Expires          = -1;
            this.Response.ContentType = "application/json";
            if (!this.Model.Succeeded)
            {
                this.Response.Write(string.Format("{{'ErrorMsg' : '{0}'}}", Business.Common.Constants.FailedUploadMessage));
                this.Response.End();
            }
            else
            {
                this.Response.Write("{}");
                this.Response.End();
            }
        }