Validate Streamreader twice

Asked

Viewed 61 times

2

I get a. csv file in my Controller. This file may 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) { ... }

always returns -1 and I can’t get inside the validations.

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

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

and use each of them in a method, but it still doesn’t work.

  • At the end of the first method that makes the validation include model.Arquivo.Inputstream.Position = 0; , and it worked

1 answer

1


As the questioner comments, there is a problem of Position when reading the file submitted by form:

model.Arquivo.InputStream.Position = 0;

This is because

model.Arquivo.Peek() == -1

Which indicates that the read position of the file is always at the end of it after the end of the first method.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.