Validate StreamReader twice

2

I get a .csv file in my Controller . This file can contain two templates , with 2 or 5 lines.

First I have to do the validation to check if the file contains 5 lines:

 var arquivo = new StreamReader(model.Arquivo.InputStream, Encoding.UTF7);
                var arquivoValida = new StreamReader(model.Arquivo.InputStream, Encoding.UTF7);

   if (this.TipoArquivoLCCP(arquivoValida, out listaRetorno, out numColunasLCCP, out isLCCP)) { }


 private bool TipoArquivoLCCP(StreamReader arquivoLCCP, out object listaRetorno, out int numColunasLCCP, out bool isLCCP)
 {

     while (arquivoLCCP.Peek() > -1)
     { /* Verificação */ }

If this check returns false , I go to the second method that does the other check:

  if (!isLCCP && this.ValidarArquivo(arquivo, model.TipoPagina, model.Pais, out listaRetorno, out numColunas))
  { ... }

Method ValidarArquivo() :

 private bool ValidarArquivo(StreamReader arquivo, Enumeradores.TipoPaginaUploadColeta tipoPagina, string idPais, out object listaRetorno, out int numColunas)
 {

     while (arquivo.Peek() > -1)
     { ... }

The problem is that the command

 while (arquivo.Peek() > -1) { ... }

It always returns -1 and I can not get into the validations.

As you can see, I created two different variables that get my file:

var arquivo = new StreamReader(model.Arquivo.InputStream, Encoding.UTF7);
var arquivoValida = new StreamReader(model.Arquivo.InputStream, Encoding.UTF7);

I use each of them in a method, but it still does not work.

    
asked by anonymous 28.04.2017 / 22:23

1 answer

1

According to the author's comment , there is a problem with Position at the time of reading the file submitted by form:

model.Arquivo.InputStream.Position = 0;

This is because

model.Arquivo.Peek() == -1

This indicates that the file reading position is always at the end of the file after the end of the first method.

    
05.05.2017 / 00:22