示例#1
0
        private void SaveFile(int userId, int schoolId, Academic.DbEntities.User.Users savedUser)
        {
            using (var helper = new DbHelper.User())
                using (var fileHelper = new DbHelper.WorkingWithFiles())
                {
                    //var savedUser = helper.AddOrUpdateUser(createdUser, cmbRole.SelectedValue, FileUpload1.PostedFile);

                    //if (savedUser != null)
                    //{
                    //public bool UploadToFolder(HttpPostedFileBase file)
                    //{
                    //    var filename = Path.GetFileName(file.FileName);
                    //    var path = Path.Combine(Server.MapPath("~/Content/Upload"), filename);
                    //    file.SaveAs(path);
                    //    return true;
                    //}

                    //save image
                    //first entry to database : table File --its image
                    if (FileUpload1.HasFile)
                    {
                        var imageFile = FileUpload1.PostedFile;

                        var userPhotoFolder = fileHelper.GetUserPhotoFolder(schoolId);
                        if (userPhotoFolder != null)
                        {
                            var image = new Academic.DbEntities.UserFile()
                            {
                                CreatedBy = userId
                                ,
                                CreatedDate = DateTime.Now
                                ,
                                DisplayName = Path.GetFileName(imageFile.FileName)
                                ,
                                FileDirectory = DbHelper.StaticValues.UserImageDirectory
                                ,
                                FileName =
                                    Guid.NewGuid().ToString() + GetExtension(imageFile.FileName, imageFile.ContentType)
                                ,
                                FileSizeInBytes = imageFile.ContentLength
                                ,
                                FileType = imageFile.ContentType
                                ,
                                IsServerFile = true
                                ,
                                SchoolId = schoolId
                                ,
                                FolderId = userPhotoFolder.Id
                            };
                            using (var fhelper = new DbHelper.WorkingWithFiles())
                            {
                                GetNewGuid(fhelper, image);
                                //TrimFirstLetterFromImageFileName(image);
                                if (trimLoop > 9 || guidLoop > 9)
                                {
                                    //cancel all save
                                }
                                else
                                {
                                    var savedFile = fhelper.AddOrUpdateFile(image);

                                    if (savedFile != null)
                                    {
                                        //save the image with this name
                                        //var filename = Path.GetFileName(file.FileName);
                                        var path = Path.Combine(Server.MapPath(DbHelper.StaticValues.UserImageDirectory),
                                                                image.FileName);
                                        imageFile.SaveAs(path);

                                        //add the image Id to user
                                        helper.UpdateUsersImage(savedUser.Id, savedFile.Id);
                                    }
                                }
                            }
                        }
                        else
                        {
                            lblSaveError.Visible = true;
                            lblSaveError.Text    = "'User Photos' directory not found.";
                        }
                        if (savedUser != null)
                        {
                            ResetTextAndCombos();
                        }
                    }
                    //else
                    //    label.Text = "Error while saving.";
                    //}
                }
        }
示例#2
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var user = Page.User as CustomPrincipal;
            if (user != null)
            {
                using (var fileHelper = new DbHelper.WorkingWithFiles())
                using (var helper = new DbHelper.User())
                {
                    if (helper.DoesUserNameExist(user.SchoolId,UserId, txtUserName.Text))
                    {
                        valiUserName.ErrorMessage = "Username already exists";
                        valiUserName.IsValid = false;
                    }
                    if (Page.IsValid)
                    {
                        var dob = DateTime.MinValue;
                        try
                        {
                            dob = Convert.ToDateTime(txtDOB.Text);
                        }
                        catch
                        {
                        }
                        var date = DateTime.Now.Date;
                        var createdUser = new Academic.DbEntities.User.Users()
                        {
                            SchoolId = user.SchoolId,
                            City = txtCity.Text,
                            Country = txtCountry.Text,
                            CreatedDate = date,
                            Email = txtEmail.Text,
                            FirstName = txtFirstName.Text,
                            MiddleName = txtMidName.Text,
                            LastName = txtLastName.Text,
                            IsActive = true,
                            IsDeleted = false,
                            UserName = txtUserName.Text,
                            Password = txtPassword.Text,
                            Id = UserId,
                            Phone = txtPhone1.Text,
                            //Description = txtDescription.Text,
                        };
                        if (cmbGender.SelectedValue!=""&& cmbGender.SelectedValue!="0")
                        {
                            createdUser.GenderId = Convert.ToInt32(cmbGender.SelectedValue);
                        }
                        if (dob != DateTime.MinValue)
                            createdUser.DOB = dob;

                        var userPhotoDirectory = fileHelper.GetUserPhotoFolder(user.SchoolId);
                        if (userPhotoDirectory != null)
                        {
                            var files = FilesDisplay.GetFiles();
                            UserFile image = null;
                            if (files != null)
                            {
                                if (files.Count >= 1)
                                {

                                    var f = files[0];
                                    //foreach (var f in files)
                                    {
                                        var fileName = Path.GetFileName(f.FilePath);
                                        image = new Academic.DbEntities.UserFile()
                                        {
                                            Id = f.Id,
                                            CreatedBy = user.Id,
                                            CreatedDate = DateTime.Now,
                                            DisplayName = f.FileDisplayName, //Path.GetFileName(imageFile.FileName)
                                            FileDirectory = DbHelper.StaticValues.UserImageDirectory,
                                                //StaticValue.UserImageDirectory
                                            FileName = fileName,
                                                //Guid.NewGuid().ToString() + GetExtension(imageFile.FileName, imageFile.ContentType)
                                            FileSizeInBytes = f.FileSizeInBytes, //imageFile.ContentLength
                                            FileType = f.FileType, //imageFile.ContentType
                                            IconPath = f.IconPath,
                                            SchoolId = user.SchoolId,
                                            IsServerFile = true,
                                            IsConstantAndNotEditable = false,
                                            FolderId = userPhotoDirectory.Id,
                                            //SubjectId = SubjectId,
                                        };
                                        if (f.Id > 0)
                                        {
                                            image.ModifiedBy = user.Id;
                                            image.ModifiedDate = DateTime.Now;
                                        }
                                    }
                                }

                            }

                            //var userRole = new Academic.DbEntities.User.UserRole()
                            //{
                            //    RoleId = Convert.ToInt32(ddlRole.SelectedValue),
                            //    UserId = UserId
                            //};

                            var savedUser = helper.AddOrUpdateUser(createdUser, ddlRole.SelectedValue, image);

                            if (savedUser != null)
                            {
                                if (UserId > 0)
                                {
                                    Response.Redirect("~/Views/User/Detail.aspx?uId=" + UserId);
                                }
                                else
                                {
                                    Response.Redirect("List.aspx");
                                }
                            }
                        }
                        else
                        {
                            //show error of "Folder unable to find"
                            lblSaveStatus.Text = "'User Photos' directory not found.";
                            lblSaveStatus.Visible = true;

                        }


                    }
                }
            }
        }