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 ?
– Dev
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.
– Marcell Alves
Um in your code I’m not seeing file closure after your opening, maybe that’s it
– Dev
Possible duplicate of txt file opens in the browser instead of being downloaded
– Marcell Alves