How to pass a txt file to a Stream type parameter?

Asked

Viewed 665 times

4

How to pass a file txt for a Stream type parameter?

That’s the ActionResult which calls the method to read the file txt:

[HttpPost]
public ActionResult Incluir(ContaReceberViewModel pContaReceber,
                            HttpPostedFileBase fileRetorno)
{
    try
    {
        var fileName = Path.GetFileName(fileRetorno.FileName);
        var _arquivo = Path
           .Combine(Server.MapPath("~/Content/Uploads/Importacao"),
                   fileName); 
        ArquivoRetornoCNAB400 cnab400 = new ArquivoRetornoCNAB400();
        //O erro ocorre aqui, quando tento passar o parâmetro:                
        cnab400.LerArquivoRetorno(bco, new FileStream(_arquivo, FileAccess.Write));
        return View(new ContaReceber());
    }
    catch (Exception)
    {
        TempData["mensagemErro"] = 
          string.Format("Ocorreu um erro ao carregar 
                      tela de importação de arquivos bancários.");
        return View(new ContaReceber());
    }
}

This is the method that will read the txt file:

public override void LerArquivoRetorno(IBanco banco, Stream arquivo)
{
    try
    {
        StreamReader stream = new StreamReader(arquivo, System.Text.Encoding.UTF8);
        string linha = "";

        // Lendo o arquivo
        linha = stream.ReadLine();

        // Próxima linha (DETALHE)
        linha = stream.ReadLine();

        while (DetalheRetorno.PrimeiroCaracter(linha) == "1")
        { 
            DetalheRetorno detalhe = banco.LerDetalheRetornoCNAB400(linha);
            ListaDetalhe.Add(detalhe);
            OnLinhaLida(detalhe, linha);
            linha = stream.ReadLine();
        }

        stream.Close();
    }
    catch (Exception ex)
    {
        throw new Exception("Erro ao ler arquivo.", ex);
    }
}         

Error:

The best overloaded method match for 'System.IO.FileStream.FileStream(string, System.IO.FileMode)' has some invalid arguments C:\PROJETOS\SistemaBeta.Web\Controllers\ImportaArquivoConciliacaoController.cs 168

  • Not what you’re doing, maybe passing a string, a file cannot be passed as parameter. Then pass a Stream, as you learned in your previous question.

  • have tried using String and not var?

  • bigown I edited the question to get better understanding, the previous question was a Console Aplicattion application and this question and a MVC web application I tried to do the same things and gives error

  • Marciano, Andrade I switched to String and gives error: 'The best overloaded method match for 'Boletonet.AbstractArquivoRetorno.Lerarquivoretorno(Boletonet.Ibanco, System.IO.Stream)' has some invalid Arguments'

  • Why don’t you do the reading with the System.IO.File.Readalllines ? string[] lines = System.IO.File.ReadAllLines(@"C:\arquivo.txt", System.Text.Encoding.UTF8);
foreach (string line in lines)
{

}

  • I think you’re complicating needlessly. This library reads and writes layout files.

  • Marconcilio Souza as I would pass this 'String Lines' to the method 'Lerarquivoretorno(Ibanco Bank, Stream file)', in the passage generates this error: 'The best overloaded method match for 'Boletonet.AbstractArquivoRetorno.Lerarquivoretorno(Boletonet.Ibanco, System.IO.Stream)' has some invalid Arguments'

Show 2 more comments

1 answer

1

Worked using using:

using (var fileStream1 = new FileStream(_arquivo, FileMode.Open))
{
    cnab400.LerArquivoRetorno(bco, fileStream1);
}

Browser other questions tagged

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