Exemple #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.";
                    //}
                }
        }
        private void SaveFile(int userId, Academic.DbEntities.User.Users savedUser)
        {
            using (var helper = new DbHelper.User())
            {
                //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 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
                        ,
                    };
                    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);


                                //    return true;
                            }
                        }
                    }
                }
                //}

                //Label label = (Label)this.Page.FindControl("lblBodyMessage");
                //if (label != null)
                //{
                if (savedUser != null)
                {
                    //label.Text = "Save Successful.";
                    //Page.Response.Redirect("List.aspx");

                    ResetTextAndCombos();
                }
                //else
                //    label.Text = "Error while saving.";
                //}
            }
        }