Click here to Skip to main content
15,901,505 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i want the download path to be that of the Application user system and now the server where the application is deployed.

the problem right now is that after deploying the application, while clicking on the export button it download straight to the server instead of the computer where im logging in from.

Kindly help with the path so it could always go to the user system.

thanks

What I have tried:

C#
protected void BtnExport_Click1(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(Cs))
        {
            conn.Open();
            if (ddlSearchGridview.SelectedValue == "-1")
            {
                lblMessage.Text = "Select A Status to Export";
                
            }
            else
            {
                try
                {
                    SqlDataAdapter sda = new SqlDataAdapter("GetDataToExportToCsv", conn);
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                    sda.SelectCommand.Parameters.AddWithValue("@complaintStatus", ddlSearchGridview.SelectedValue);

                    DataTable dt = new DataTable();
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter();
                    

                    //sda.Fill(ds);
                    sda.Fill(dt);

                    path = "C:\\excel\\CBN_CCMS_" + DateTime.Now.ToString("ddMMyyyy") + DateTime.Now.Millisecond + ".csv";
                   // CreateExcelFile.CreateExcelDocument(ds, path);
                    CreateCSVFile(dt, path);

                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
                finally
                {
                    conn.Close();
                }
            }
            
        }
    }
Posted
Updated 8-Aug-16 1:15am
v2

Welcome Emmablakes

You will have to let the user choose a file save location and filename.
The solution is as follows: (sorry I dummied some code to get a quick one working on my machine for you)

C#
using System;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Text;
using System.Web.UI;

namespace WebFormsGridViewCommand
{
    public partial class _Default : Page
    {
        private string Cs = @"data source=(localdb)\mssqllocaldb;initial catalog=webformstest;integrated security=true";
        private string path;

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnExport_Click(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(Cs))
            {
                conn.Open();
                //if (ddlSearchGridview.SelectedValue == "-1")
                //{
                //    lblMessage.Text = "Select A Status to Export";

                //}
                //else
                //{
                try
                {
                    SqlDataAdapter sda = new SqlDataAdapter("GetDataToExportToCsv", conn);
                    sda.SelectCommand.CommandType = CommandType.StoredProcedure;
                    //sda.SelectCommand.Parameters.AddWithValue("@complaintStatus", ddlSearchGridview.SelectedValue);

                    DataTable dt = new DataTable();
                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter();


                    //sda.Fill(ds);
                    sda.Fill(dt);

                    path = "C:\\excel\\CBN_CCMS_" + DateTime.Now.ToString("ddMMyyyy") + DateTime.Now.Millisecond + ".csv";
                    // CreateExcelFile.CreateExcelDocument(ds, path);
                    CreateCSVFile(dt, path);

                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
                finally
                {
                    conn.Close();
                }
                //}

            }
        }

        private void CreateCSVFile(DataTable dt, string path)
        {
            var sb = new StringBuilder();

            // Add dt columns as csv first row headers:
            foreach (DataColumn dc in dt.Columns)
            {
                sb.Append(dc.ColumnName).Append(",");
            }
            sb.Remove(sb.Length - 1, 1);
            sb.AppendLine();

            // Add rows data to csv
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                foreach (DataColumn dc in dt.Columns)
                {
                    sb.Append(dt.Rows[i][dc.ColumnName]).Append(",");
                }
                sb.Remove(sb.Length - 1, 1);
                sb.AppendLine();
            }

            // Push csv string to server response for user to download
            string filename = Path.GetFileName(path); // Get the filename part only as the user will be asked where to save.

            Response.Clear(); // <- this will force the asp.net response to only deliver the csv file on button click, not html.

            // this tells the browser that it needs to prompt the user for a file save
            Response.AddHeader("content-disposition", string.Format("attachment;filename={0}", filename));

            // This tells the browser the file type, in case it has built in viewer.
            Response.ContentType = "application/csv";

            // Sends the csv file content to the http response
            Response.Write(sb.ToString());
        }
    }
}
 
Share this answer
 
Comments
Emmablakes 8-Aug-16 7:10am    
thanks njammy... exactly what i needed.
njammy 8-Aug-16 7:23am    
You're welcome, if you get a chance please rate my answer too using the stars to help others find the solution more efficiently.
If this is a web based server system, then you can't specify a client destination: you can create the file in the way you show, but it will always be saved on the server because Server code has no direct access to the Client file system.
To send it to the client, you would need to use Response.WriteFile (there is an example here: asp.net - ASP C# Send File to Client - Stack Overflow[^]) and let the client decide where to put it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900