Пример #1
0
        protected void btnSave_Click(object sender, EventArgs e)
        {
            string idPerson = this.FormContext.QueryString["idResource"].ToString();

            foreach (GridViewRow row in this.gvUploadedFiles.Rows)
            {
                if (row.RowType == DataControlRowType.DataRow)
                {
                    //ID на прикачени файл
                    HiddenField hdnRowMasterKey = row.FindControl("hdnIdUploadedFile") as HiddenField;

                    //radio button
                    RadioButton rbtn = row.FindControl("rbtnSelectedFile") as RadioButton;

                    //описание за всеки прикачен файл
                    TextBox tbxDescription = row.Cells[0].FindControl("tbxDescription") as TextBox;
                    string  descr          = tbxDescription.Text;

                    //Update на всички редове като записваме само описанието
                    this.currentEntity        = CommonClientRef.GetUploadFileByID(hdnRowMasterKey.Value);
                    currentEntity.Description = descr;

                    CallContext resultContext = new CallContext();
                    resultContext.CurrentConsumerID = this.UserProps.IdUser;
                    resultContext = CommonClientRef.UploadedFileSave(currentEntity, resultContext);

                    //update на снимката на потребителя
                    if (rbtn.Checked)
                    {
                        this.personEntity = this.AdminClientRef.GetPersonByPersonID(idPerson);
                        if (this.personEntity != null)
                        {
                            this.personEntity.ImagePath = this.currentEntity.FilePath.Replace("\\", "/").Replace("C:", string.Empty);

                            CallContext resultPersontContext = new CallContext();
                            resultPersontContext.CurrentConsumerID = this.UserProps.IdUser;
                            resultPersontContext = AdminClientRef.PersonSave(this.personEntity, resultPersontContext);
                        }

                        //refresh Parent form
                        string script = "this.window.opener.location=this.window.opener.location;";
                        if (!ClientScript.IsClientScriptBlockRegistered("REFRESH_PARENT"))
                        {
                            ClientScript.RegisterClientScriptBlock(typeof(string), "REFRESH_PARENT", script, true);
                        }
                    }
                }
            }
        }
        protected void btnUpload_Click(object sender, EventArgs e)
        {
            //update на снимката на потребителя
            if (!string.IsNullOrEmpty(this.hdnRowMasterKey.Value) && this.hdnRowMasterKey.Value != Constants.INVALID_ID_STRING)
            {
                string idPerson = this.hdnRowMasterKey.Value;
                this.currentEntity = this.ownerPage.AdminClientRef.GetPersonByPersonID(idPerson);

                //създава и отваря ресурсна папка с име - idPerson_PersonName
                string folderName = this.hdnRowMasterKey.Value + "_" + BaseHelper.ConvertCyrToLatin(this.currentEntity.FirstName.Trim());

                string resourcesFolderName = GeneralPage.GetSettingByCode(ETEMEnums.AppSettings.ResourcesFolderName).SettingValue + @"\Person\";

                //ID с което започва папката за импорт. Пример C:\Resources_UMS_Person\198_Kiril
                string idStartFolder = folderName.Split('_')[0].ToString();

                DirectoryInfo folder = new DirectoryInfo(resourcesFolderName);// + folderName);

                //Винаги изтриваме целевата папка за да не се пълни с всяка следваща снимка
                if (folder.Exists)
                {
                    DirectoryInfo[] directories = folder.GetDirectories();

                    foreach (var file in directories)
                    {
                        if (file.Name.StartsWith(idStartFolder + "_"))
                        {
                            FileInfo[] filesToDelete = file.GetFiles();
                            foreach (var delFile in filesToDelete)
                            {
                                File.Delete(delFile.FullName);
                            }

                            //Directory.Delete(file.FullName);

                            break;
                        }
                    }
                }

                //и отново създаваме потребителската директория
                folder = new DirectoryInfo(resourcesFolderName + folderName);

                if (!folder.Exists)
                {
                    folder = Directory.CreateDirectory(resourcesFolderName + folderName);
                }


                //ако сме избрали нещо
                if (!string.IsNullOrEmpty(FileUpload1.FileName))
                {
                    //записваме картинката в папката
                    string pathToSave = (folder.FullName.EndsWith("\\") ? folder.FullName : folder.FullName + "\\") + FileUpload1.FileName;

                    FileUpload1.SaveAs(pathToSave);

                    //update Person
                    if (this.currentEntity != null)
                    {
                        this.currentEntity.ImagePath = GeneralPage.GetSettingByCode(ETEMEnums.AppSettings.WebResourcesFolderName).SettingValue + "/Person/" + folderName + "/" + FileUpload1.FileName;
                        CallContext resultPersontContext = new CallContext();
                        resultPersontContext.CurrentConsumerID = idPerson;
                        resultPersontContext = AdminClientRef.PersonSave(this.currentEntity, resultPersontContext);
                    }
                }

                this.CurrentEntityMasterID = idPerson;
            }

            this.pnlAddPersonImage.Visible = false;
        }