I use the following code to generate a txt file and send it to my View
:
public ActionResult geraBpa(){
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);
}
The same is working properly, however, instead of being downloaded, it opens on the browser screen.
Any suggestions?