コード例 #1
0
        /// <summary>
        /// Дабавить/редактировать
        /// </summary>
        /// <param name="name">Наимование</param>
        /// <param name="text">Тест статьи</param>
        /// <param name="author">Автор</param>
        /// <param name="headingID">Ссылка на рубрику</param>
        /// <param name="nameFile">Наимование файла</param>
        /// <param name="file">Файл</param>
        /// <param name="isDeleteFile">Удаление файла при редактирование</param>
        /// <param name="isNew">Новый</param>
        private void SetValue(string name, string text, string author, int headingID,
                              string nameFile, byte[] file, bool isDeleteFile, bool isNew = true)
        {
            WorkForFiles workForFiles = WorkForFiles.New;

            if (isNew)
            {
                article            = new Article();
                article.DateCreate = DateTime.Now;
                workForFiles       = WorkForFiles.New;
            }
            else
            {
                workForFiles = WorkForFiles.Edit;
                nameFile     = "foto" + article.ID.ToString() + nameFile;
            }

            article.Name   = name.Trim();
            article.Text   = text.Trim();
            article.Author = author.Trim();

            article.HeadingID = headingID;
            if (isNew)
            {
                dataContent.Articles.Add(article);
            }
            dataContent.SaveChanges();
            if (workForFiles == WorkForFiles.New)
            {
                nameFile = "foto" + article.ID.ToString() + nameFile;
                if (file?.Length > 0)
                {
                    article.FileName = nameFile;
                }
                else
                {
                    article.FileName = string.Empty;
                }
                dataContent.SaveChanges();
                if (file?.Length > 0)
                {
                    WorkForFile(nameFile, string.Empty, file, isDeleteFile, WorkForFiles.New);
                }
            }
            else
            {
                WorkForFile(nameFile, article.FileName, file, isDeleteFile, WorkForFiles.Edit);
                if (file?.Length > 0 && (article.FileName == null || article.FileName == string.Empty))
                {
                    article.FileName = nameFile;
                    dataContent.SaveChanges();
                }
                else if (isDeleteFile)
                {
                    article.FileName = string.Empty;
                    dataContent.SaveChanges();
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Дабавить/редактировать
        /// </summary>
        /// <param name="login">Логин</param>
        /// <param name="isPaswordChange">Изменить пароль</param>
        /// <param name="password">пароль</param>
        /// <param name="passwordReplay">Повторный пароль для проверки при изменение</param>
        /// <param name="oldPassword">Старый пароль при изменение</param>
        /// <param name="role">Роль</param>
        /// <param name="surName">Фамилия</param>
        /// <param name="middleName">Отчество</param>
        /// <param name="file">Файл</param>
        /// <param name="isDeleteFile">Удаление файла при редактирование</param>
        /// <param name="isNew">Новый</param>

        private void SetValue(string login, bool isPaswordChange, string password, string passwordReplay,
                              string oldPassword, RoleInfo role, string surName, string name, string middleName,
                              byte[] file, bool isDeleteFile, bool isNew = true)
        {
            WorkForFiles workForFiles = WorkForFiles.New;

            if (isNew)
            {
                user         = new User();
                workForFiles = WorkForFiles.New;
            }
            else
            {
                workForFiles = WorkForFiles.Edit;
            }

            user.Login = login.GetNotNull();
            if (isPaswordChange)
            {
                user.Password = PasswordEncryption(password.GetNotNull());
            }
            if (role != null)
            {
                user.RoleID = role.ID;
            }
            user.Name       = name.GetNotNull();
            user.SurName    = surName.GetNotNull();
            user.MiddleName = middleName.GetNotNull();

            if (isNew)
            {
                dataContent.Users.Add(user);
            }
            dataContent.SaveChanges();
            if (workForFiles == WorkForFiles.New)
            {
                user.IsPhoto = (file?.Length > 0);
                if (file?.Length > 0)
                {
                    WorkForFile(login, file, isDeleteFile, WorkForFiles.New);
                }
            }
            else
            {
                if (file?.Length > 0)
                {
                    user.IsPhoto = true;
                    dataContent.SaveChanges();
                }
                else if (isDeleteFile)
                {
                    user.IsPhoto = false;
                    dataContent.SaveChanges();
                }
                WorkForFile(login, file, isDeleteFile, WorkForFiles.Edit);
            }
        }
コード例 #3
0
        /// <summary>
        /// Работа с файлами
        /// </summary>
        /// <param name="nameFile">Наимование файла</param>
        /// <param name="oldName">Предыдущее наименование из БД для удалеия</param>
        /// <param name="file">Файл</param>
        /// <param name="isDelete">Удаление файла при редактирование</param>
        /// <param name="workForFile">Что нужно сделать</para>
        private void WorkForFile(string nameFile, string oldName, byte[] file, bool isDelete, WorkForFiles workForFile)
        {
            Configuration cfg = null;

            if (HttpContext.Current != null)
            {
                cfg =
                    System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
            }
            else
            {
                cfg =
                    ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            }
            MediaFolderConfigSection section = (MediaFolderConfigSection)cfg.GetSection("MediaFolder");

            if (section == null)
            {
                return;
            }
            string pathDir = section.FolderItems[0].Path;

            pathDir = HostingEnvironment.MapPath(pathDir);

            switch (workForFile)
            {
            case WorkForFiles.New:
            {
                if (file?.Length > 0)
                {
                    //File.WriteAllBytes(pathDir + nameFile, file);
                    using (var stream = new FileStream(pathDir + nameFile, FileMode.Create))
                    {
                        stream.Write(file, 0, file.Length);
                    }
                }

                break;
            }

            case WorkForFiles.Edit:
            {
                if (isDelete && oldName?.Length > 0)
                {
                    File.Delete(pathDir + oldName);
                }
                else if (file?.Length > 0 && nameFile?.Length > 0)
                {
                    if (oldName == null || oldName == string.Empty)
                    {
                        oldName = nameFile;
                    }
                    //if (oldName?.Length > 0)
                    //File.Delete(pathDir + oldName);
                    File.WriteAllBytes(pathDir + oldName, file);
                }
                break;
            }

            case WorkForFiles.Delete:
            {
                if (oldName?.Length > 0)
                {
                    File.Delete(pathDir + oldName);
                }
                break;
            }
            }
        }