View error returning a txt file

Asked

Viewed 51 times

0

I’m using the following code in my Controller:

public ActionResult geraBpa()
{
    var caminho = System.Web.HttpContext.Current.Server.MapPath("~/Content");
    StreamWriter file = new StreamWriter($"{caminho}/BPA.txt");
    List<bpac> listaBpac = pegaBpac();

    int linhaTexto = 1;
    int linhaItem = 1;

    foreach (bpac linha in listaBpac)
    {
        file.WriteLine(
        "02" +
        linha.cnes +
        linha.cmp + //competencia
        linha.cbo +
        string.Format("{0:000}", linhaTexto) + string.Format("{0:00}", linhaItem) +
        linha.pa +
        "000" +
        string.Format("{0:000000}", linha.quant) +
        "EXT"
        );

        linhaItem++;
        if (linhaItem > 99)
        {
            linhaItem = 1;
            linhaTexto++;
        }
    }

    linhaTexto++;
    linhaItem = 1;

    byte[] fileBytes = System.IO.File.ReadAllBytes($"{caminho}/BPA.txt");
    string fileName = "myfile.ext";
    return File(fileBytes, MediaTypeNames.Application.Octet, fileName);
}

I’m getting error on this line:

byte[] fileBytes = System.IO.File.Readallbytes($"{path}/BPA.txt");

The error says that the file BPA.txt is open. When I went to the folder, I saw that it was not created properly.

Some help?

  • Please could post the error that is giving ?

  • How big is the file? This response from the English OS suggests that there is a reading limitation using this method: https://stackoverflow.com/questions/2030847/best-way-to-read-a-large-file-into-a-byte-array-in-c. Consider using a Filestream.

  • 1

    Um in your code I’m not seeing file closure after your opening, maybe that’s it

  • 3

1 answer

4


This is exactly the problem, the file is already open when reading attempt is made.

Your code opens a StreamWriter and does not close it. You can use the method Dispose or put the code that uses the variable file within a using. You can read about the using in What is the usefulness of using?

I’d go the second way, the code would look like this

var caminho = System.Web.HttpContext.Current.Server.MapPath("~/Content");
using(StreamWriter file = new StreamWriter($"{caminho}/BPA.txt"))
{
    List<bpac> listaBpac = pegaBpac();

    int linhaTexto = 1;
    int linhaItem = 1;

    foreach (bpac linha in listaBpac)
    {
        file.WriteLine(
        "02" +
        linha.cnes +
        linha.cmp + //competencia
        linha.cbo +
        string.Format("{0:000}", linhaTexto) + string.Format("{0:00}", linhaItem) +
        linha.pa +
        "000" +
        string.Format("{0:000000}", linha.quant) +
        "EXT"
        );

        linhaItem++;
        if (linhaItem > 99)
        {
            linhaItem = 1;
            linhaTexto++;
        }
    }
}

linhaTexto++;
linhaItem = 1;

byte[] fileBytes = System.IO.File.ReadAllBytes($"{caminho}/BPA.txt");
string fileName = "myfile.ext";
return File(fileBytes, MediaTypeNames.Application.Octet, fileName);
  • Hi, the code worked, but it opens on the screen instead of downloading. Any suggestions?

  • Yes. Some suggestions. Don’t you think you should open another question? To be honest, I think it’s important because I’d like to have this subject catalogued somewhere.

  • Blz, I will open.

  • https://answall.com/questions/231227/arquivo-txt-abre-no-navigator-inv%C3%a9s-to-be-downloaded

Browser other questions tagged

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