0

I've been given a task to export data into the csv file. every thing is working fine apart from the destination folder. Every time it saves csv file on windows downloads (C:\Users\xxxpurt\Downloads) folder. I want to use a desired location to save the csv file by specifying the location which I can get it from SaveFileDialog. Is this possible? If it is then how can I specify path retrieved from saveFiledialog to response? Ta

string location = string.Empty;
SaveFileDialog saveCSVDialog = new SaveFileDialog();

saveCSVDialog.InitializeLifetimeService();
saveCSVDialog.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
saveCSVDialog.FilterIndex = 1;
saveCSVDialog.DefaultExt = ".csv";
saveCSVDialog.RestoreDirectory = true;

DialogResult res = STAShowDialog(saveCSVDialog); //STAShowDialog uses threading

if (res == DialogResult.OK)
{
   location = saveCSVDialog.FileName;
}

Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}",        Server.HtmlEncode(location)));
Response.Charset = "";
Response.ContentType = "application/text";

.........Fetch columns and rows using loops.........

Response.Output.Write(sb.ToString());
Response.Flush();
Response.End();
xxxpurt
  • 1
  • 1
  • 3
  • What are you trying to do? You are trying to decide on the server where the file will be stored on the client? That's not possible – TimothyP Jan 11 '13 at 10:16
  • 1
    before the download the client will decide where to save it, you can't do it from code behind. – GeorgesD Jan 11 '13 at 10:20
  • ok. so is there no way to tell the response to write the file on the specific path? only filename is permitted? cheers – xxxpurt Jan 11 '13 at 10:24
  • Exactly.. you have no access to the filesystem of the client computer, you do not even know what it looks like and if it's even a computer. If you REALLY want to decide where it is stored on the client, you create a special client application and use something other than a basic html page – TimothyP Jan 11 '13 at 10:25
  • exactly that is what I was doing. let the client (IE) to decide where to store the file but its the bloody requirement from the user to choose the path where to store the file. – xxxpurt Jan 11 '13 at 10:27
  • So what you really want is to make sure IE shows a Save File Dialog on the client and doesn't just store it in the default download folder? – TimothyP Jan 11 '13 at 10:28

2 Answers2

0

As stated in the comments you cannot decide on the server where to store the file on the client. And you really shouldn't create UI elements in your server code either.

If you want to force a save file dialog on the client you can try this:

Response.AppendHeader("content-disposition", "attachment; filename=somefile.csv");
Response.ContentType = "text/csv";

However, ultimately it's up to the client browser to decide what it does with it, most will show a save file dialog though.

TimothyP
  • 21,178
  • 26
  • 94
  • 142
-1

put

 SaveFileDialog saveCSVDialog = new SaveFileDialog();

as class property and

saveCSVDialog.InitializeLifetimeService();
saveCSVDialog.Filter = "csv files (*.csv)|*.csv|All files (*.*)|*.*";
saveCSVDialog.FilterIndex = 1;
saveCSVDialog.DefaultExt = ".csv";
saveCSVDialog.RestoreDirectory = true;

in class constructor

because if you put them in one method, everytime you call that method you will get new instance of SaveFileDialog

dandice
  • 179
  • 3
  • 13