Ejemplo n.º 1
0
        protected void btnConfirmar_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(hdnArq.Value) && string.IsNullOrEmpty(fupAnexo.FileName)) // Nao foi tirada nenhuma foto ou carregada.
            {
                if (imgAntiga.Visible)
                {
                    __SessionWEB.PostMessages = UtilBO.GetErroMessage("Foto do aluno salva com sucesso.", UtilBO.TipoMensagem.Sucesso);
                    Redireciona();
                }
                else
                {
                    lblMessage.Text = UtilBO.GetErroMessage("Nenhuma imagem foi carregada.", UtilBO.TipoMensagem.Alerta);
                }
            }
            else
            {
                try
                {
                    // Se houver imagem capturada e escolhida para upload, salvará a que foi capturada.
                    if (!string.IsNullOrEmpty(fupAnexo.FileName) && string.IsNullOrEmpty(hdnArq.Value))
                    {
                        HttpPostedFile arquivo = fupAnexo.PostedFile;

                        if (arquivo != null && arquivo.ContentLength > 0)
                        {
                            string fileNameApplication = System.IO.Path.GetFileName(arquivo.FileName);

                            if (fileNameApplication != String.Empty)
                            {
                                if (fupAnexo.PostedFile.FileName.Substring(fupAnexo.PostedFile.FileName.Length - 3, 3).ToUpper() != "JPG")
                                {
                                    throw new ValidationException("Foto tem que estar no formato \".jpg\".");
                                }

                                SYS_Arquivo entArquivo = SYS_ArquivoBO.CriarAnexo(arquivo);
                                entArquivo.arq_data = ACA_AlunoBO.RedimensionaFoto(entArquivo.arq_data, true);

                                SYS_ArquivoBO.Save(entArquivo);
                                VS_arq_id = entArquivo.arq_id;
                            }
                        }


                        string tam = SYS_ParametroBO.ParametroValor(SYS_ParametroBO.eChave.TAMANHO_MAX_FOTO_PESSOA);

                        if (!string.IsNullOrEmpty(tam))
                        {
                            if (fupAnexo.PostedFile.ContentLength > Convert.ToInt32(tam) * 1000)
                            {
                                throw new ValidationException("Foto é maior que o tamanho máximo permitido.");
                            }

                            if (fupAnexo.PostedFile.FileName.Substring(fupAnexo.PostedFile.FileName.Length - 3, 3).ToUpper() != "JPG")
                            {
                                throw new ValidationException("Foto tem que estar no formato \".jpg\".");
                            }
                        }
                    }

                    if (ACA_AlunoBO.SalvarFotoAluno(VS_alu_id, VS_arq_id))
                    {
                        ApplicationWEB._GravaLogSistema(LOG_SistemaTipo.Update, "alu_id: " + VS_alu_id + ", arq_id: " + VS_arq_id);
                        __SessionWEB.PostMessages = UtilBO.GetErroMessage("Foto do aluno salva com sucesso.", UtilBO.TipoMensagem.Sucesso);
                        DeletaArquivoTemporario();

                        Redireciona();
                    }
                    else
                    {
                        lblMessage.Text = UtilBO.GetErroMessage("Não foi possível salvar a foto do aluno.", UtilBO.TipoMensagem.Erro);
                    }
                }
                catch (ValidationException ex)
                {
                    lblMessage.Text = UtilBO.GetErroMessage(ex.Message, UtilBO.TipoMensagem.Alerta);
                }
                catch (Exception ex)
                {
                    ApplicationWEB._GravaErro(ex);
                    lblMessage.Text = UtilBO.GetErroMessage("Erro ao tentar salvar foto do aluno.", UtilBO.TipoMensagem.Erro);
                }
            }
        }