コード例 #1
0
        public ActionResult UploadFiles()
        {
            List <RedesimImportFilesViewModel> Lista_Files = new List <RedesimImportFilesViewModel>();
            RedesimImportViewModel             model       = new RedesimImportViewModel()
            {
                ListaArquivo = Lista_Files
            };

            return(View(model));
        }
コード例 #2
0
        public ActionResult UploadFiles(RedesimImportViewModel model)
        {
            List <RedesimImportFilesViewModel> Lista_Files = new List <RedesimImportFilesViewModel>();
            int    _id = 0;
            string _msg = "", _tipo = "", _guid = "", _tipoSigla = "";
            bool   _ok      = false;
            var    fileName = "";

            if (model.ListaArquivo == null)
            {
                model.ListaArquivo = new List <RedesimImportFilesViewModel>();
            }

            Redesim_bll redesimRepository = new Redesim_bll(_connection);


            foreach (var file in model.Files)
            {
                List <Redesim_RegistroStruct>      _listaRegistro      = new List <Redesim_RegistroStruct>();
                List <Redesim_ViabilidadeStuct>    _listaViabilidade   = new List <Redesim_ViabilidadeStuct>();
                List <Redesim_licenciamentoStruct> _listaLicenciamento = new List <Redesim_licenciamentoStruct>();
                if (file == null)
                {
                    goto Fim;
                }
                fileName = Path.GetFileName(file.FileName);
                if (file.ContentLength > 0)
                {
                    if (file.ContentType == "application/vnd.ms-excel" || file.ContentType == "text/csv")
                    {
                        _guid = Guid.NewGuid().ToString("N");
                        string _path = "~/Files/Redesim/";
                        var    path  = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(_path), _guid);
                        file.SaveAs(path);

                        //###Verifica tipo de arquivo####
                        bool         _bRegistro = false, _bViabilidade = false, _bLicenciamento = false;
                        StreamReader reader     = new StreamReader(@path, Encoding.Default);
                        string       line       = reader.ReadLine();
                        char         _delimeter = line.Contains(";")?';':',';

                        if (!string.IsNullOrWhiteSpace(line))
                        {
                            string[] values = line.Split(_delimeter);
                            if (values[0].ToString().ToUpper() == "PROTOCOLO" && values[1].ToString().ToUpper() == "CNPJ")
                            {
                                _tipo      = "Registro";
                                _bRegistro = true;
                            }
                            else
                            {
                                if (values[0].ToString().ToUpper() == "PROTOCOLO" && values[1].ToString().ToUpper() == "ANALISE")
                                {
                                    _tipo         = "Viabilidade";
                                    _bViabilidade = true;
                                }
                                else
                                {
                                    if (values[0].ToString().ToUpper() == "PROTOCOLOLICENCA" && values[1].ToString().ToUpper() == "IDSOLICITACAO")
                                    {
                                        _tipo           = "Licenciamento";
                                        _bLicenciamento = true;
                                    }
                                }
                            }
                        }
                        reader.Close();
                        if (!_bRegistro && !_bViabilidade && !_bLicenciamento)
                        {
                            _ok  = false;
                            _msg = "Arquivo inválido";
                            goto ProximoArquivo;
                        }
                        _listaRegistro.Clear();
                        _listaViabilidade.Clear();
                        //Lê Registro
                        if (_bRegistro)
                        {
                            _listaRegistro = Read_Registro(path);
                            _listaRegistro = Insert_Registro(_listaRegistro, _guid);
                            _ok            = true;
                            _tipoSigla     = "R";
                        }

                        //Lê Viabilidade
                        if (_bViabilidade)
                        {
                            _listaViabilidade = Read_Viabilidade(path);
                            _listaViabilidade = Insert_Viabilidade(_listaViabilidade, _guid);
                            _ok        = true;
                            _tipoSigla = "V";
                        }

                        //Lê Licenciamento
                        if (_bLicenciamento)
                        {
                            _listaLicenciamento = Read_Licenciamento(path);
                            _listaLicenciamento = Insert_Licenciamento(_listaLicenciamento, _guid);
                            _ok        = true;
                            _tipoSigla = "L";
                        }


                        if (_ok)
                        {
                            Redesim_arquivo reg = new Redesim_arquivo()
                            {
                                Guid = _guid,
                                Tipo = _tipoSigla
                            };
                            Exception ex = redesimRepository.Incluir_Arquivo(reg);
                            _msg = "Arquivo importado";
                        }
                        //#####FIM ARQUIVO DE REGISTRO  #######
                    }
                    else if (file.ContentType == "text/xml")
                    {
                        _guid = Guid.NewGuid().ToString("N");
                        string _path = "~/Files/Redesim";
                        var    path  = Path.Combine(System.Web.HttpContext.Current.Server.MapPath(_path), _guid);
                        file.SaveAs(path);
                        _tipo = Read_Xml(Path.Combine(_path, _guid));
                        if (_tipo == "L")
                        {
                            _listaLicenciamento = Read_Licenciamento_Xml(Path.Combine(_path, _guid));
                            _listaLicenciamento = Insert_Licenciamento(_listaLicenciamento, _guid);
                            _ok        = true;
                            _tipoSigla = "L";
                        }
                        else if (_tipo == "R")
                        {
                            _listaRegistro = Read_Registro(Path.Combine(_path, _guid));
                            //                            _listaRegistro = Insert_Registro(_listaRegistro, _guid);
                            _ok        = true;
                            _tipoSigla = "R";
                        }
                        else
                        {
                            _ok  = false;
                            _msg = "Arquivo inválido";
                        }
                        if (_ok)
                        {
                            Redesim_arquivo reg = new Redesim_arquivo()
                            {
                                Guid = _guid,
                                Tipo = _tipoSigla
                            };
                            // Exception ex = redesimRepository.Incluir_Arquivo(reg);
                            _msg = "Arquivo importado";
                        }
                    }
                    else
                    {
                        _ok  = false;
                        _msg = "Arquivo inválido";
                    }
                }
                else
                {
                    _ok  = false;
                    _msg = "Tamanho inválido";
                }
ProximoArquivo:
                RedesimImportFilesViewModel _reg = new RedesimImportFilesViewModel()
                {
                    Guid               = _guid,
                    NomeArquivo        = fileName,
                    Mensagem           = _msg,
                    Valido             = _ok,
                    Tipo               = _tipo,
                    ListaRegistro      = _listaRegistro,
                    ListaViabilidade   = _listaViabilidade,
                    ListaLicenciamento = _listaLicenciamento
                };
                Lista_Files.Add(_reg);
                _id++;
            }
Fim:
            model.ListaArquivo = Lista_Files;
            return(View(model));
        }