1
I have a C# code that creates a txt file and then downloads it.
The code works very well on my local machine, but when uploading it to the server, it gives an error:
Non-static method requires a destination.
public ActionResult GeraBpa()
{
var caminho = System.Web.HttpContext.Current.Server.MapPath("~/Content");
int linhaTexto = 1;
int linhaItem = 1;
Int64 digito = 0;
string cmp = Request.QueryString["cmp"];
oUsuario = (usuario)Session["usuario"];
sm oSMS = modelOff.sms.SingleOrDefault(p => p.ibge == oUsuario.ibge);
//cria o arquivo txt
using (StreamWriter file = new StreamWriter($"{caminho}/PA" + oUsuario.ibge + cmp + ".txt"))
{
//cria uma lista com as informacoes que eu preciso
List<bpac> bpa = modelOff.bpacs.Where(p => p.cmp == cmp && p.ibge == oUsuario.ibge).ToList();
//preenche o arquivo txt com os dados da lista criada acima
foreach (bpac linha in bpa)
{
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"
);
digito += Convert.ToInt64(linha.pa);
digito += linha.quant;
linhaItem++;
if (linhaItem > 99)
{
linhaItem = 1;
linhaTexto++;
}
}
linhaTexto++;
linhaItem = 1;
digito = digito % 1111;
digito += 1111;
//pega todas as linhas criadas anteriormente e salva em uma lista
List<string> linhas = System.IO.File.ReadAllLines($"{caminho}/PA" + oUsuario.ibge + cmp + ".txt").ToList();
//cria uma nova linha
string primeiraLinha = "01#BPA#" +
cmp + //competencia
linhas.Count.ToString().PadLeft(6, '0') + //total de linhas
linhaTexto.ToString().PadLeft(6, '0') + //total de folhas
digito.ToString() +
oSMS.responsavel.PadRight(30, ' ') +
oSMS.sigla.PadRight(6, ' ') +
oSMS.cnpj.PadRight(14, ' ') +
"SECRETARIA MUNICIPAL DE SAUDE M " +
"";
//insere a primeira linha no topo das outras
linhas.Insert(0, primeiraLinha);
//salva novamente o arquivo
System.IO.File.WriteAllLines($"{caminho}/PA" + oUsuario.ibge + cmp + ".txt", linhas);
//disponibiliza o arquivo para download
byte[] fileBytes = System.IO.File.ReadAllBytes($"{caminho}/PA" + oUsuario.ibge + cmp + ".txt");
string fileName = "PA" + oUsuario.ibge + cmp + ".txt";
return File(fileBytes, MediaTypeNames.Application.Octet, fileName);
}
I couldn’t get a more specific error, since the error only occurs on the online server and not on my machine.
I hope you can help me.
EDITED
I changed the permissions (I put 100% access to all users), but I received access error:
Access to the 'F: Inetpub vhosts italorodrigo.com.br Bpa.italorodrigo.com.br Content PA260500201708.txt'
Detail is that the file was not created
Don’t have the line? If not, isolate to make it easier to know where the error is. It can be in several places.
– Maniero
I made an edit on the question. gave access error. but I’ve already made the permission
– Italo Rodrigo
I managed to resolve it. I had to give access to the pool group
– Italo Rodrigo
if someone who understands this can explain better what it means is good
– Italo Rodrigo