Error exporting a txt file

1

I have this code block in vs2010 with C# what it does is to create a txt file 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 the local export works and the file in the network unit is equal, but when I pass the application to the server and (IIS) I get the following error.

I do not know if it would be ok to give some permission that I will be going by ignoring

    
asked by Eduard 18.10.2017 в 19:04
source

1 answer

2

You must give access permissions (Read and Write) to the user that uses IIS to run on the computer, it is usually computername\iis_isusrs since for security reasons it does not have permissions to write to other directories . Another solution would be to write them in the app_data of the site along with a functionality that allows downloading them.

    
answered by 18.10.2017 / 19:15
source