I have this block of code in vs2010 with C#
what it does is create a file txt
to save to a network drive.
protected void export(object sender, EventArgs e)
{
ExportarEmbarque();
}
public void ExportarEmbarque()
{
string strCon6 = ConfigurationManager.ConnectionStrings["Sk_DBFrecuencias"].ConnectionString;
SqlConnection conn6 = new SqlConnection(strCon6);
SqlCommand comm6 = new SqlCommand();
comm6.Connection = conn6;
comm6.CommandText = "aqui va la consulta where f.SE_Factura= " + "'" + TxtExportFactura.Text.Trim() + "'" + " )t";
conn6.Open();
SqlDataReader nw6Reader = comm6.ExecuteReader();
conn6.Close();
string constr = ConfigurationManager.ConnectionStrings["Sk_DBFrecuencias"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("aqui va la consulta where f.SE_Factura=" + "'" + TxtExportFactura.Text.Trim() + "'"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
//Build the Text file data.
string txt = string.Empty;
foreach (DataRow row in dt.Rows)
{
foreach (DataColumn column in dt.Columns)
{
//Add the Data rows.
if (column.ColumnName.Equals("cantidadF"))
{
txt += row[column.ColumnName].ToString().Replace(",000", "").Replace(".000", "");
}
else if (column.ColumnName.Equals("cantidade"))
{
txt += row[column.ColumnName].ToString().Replace(",000", "").Replace(".000", "");
}
else if (column.ColumnName.Equals("Peso"))
{
txt += row[column.ColumnName].ToString().Replace(",000000", "").Replace(".000000", "");
}
else if (column.ColumnName.Equals("Volumen"))
{
txt += row[column.ColumnName].ToString().Replace(".", "").Replace(",", "").Remove(4, 3);
//.Remove(4,3)
}
else
{
txt += row[column.ColumnName].ToString();
}
}
//Add new line.
txt += "\r";
}
//Download the Text file.
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=" + "'" + TxtExportFactura.Text + ".txt" + "'" );
string filename = TxtExportFactura.Text + ".txt";
Response.Charset = "";
Response.ContentType = "application/text";
System.IO.File.AppendAllText(@"\\unidadserver\soc\soc\CROSS\" + filename, txt);
Response.Flush();
Response.End();
TxtExportFactura.Text = "";
}
}
}//FIN exportar plano.
}
}
The problem is that if you export it locally it works and saves the file to the network drive, but when I pass the application to the server and to (IIS) I get the following error.
I don't know if I have to give any permission that I will be ignoring
You must give access permissions (Read and write) to the user with which it is used
IIS
to run on the computer, usuallycomputername\iis_isusrs
because for security reasons it does not have permissions to write to other directories. Another solution would be to write them in the site's app_data together with a functionality that allows them to be downloaded.