I want the users to type their text in the given textbox and on clicking on createNewFile Button, a SaveAs Dialogbox should popup and the users should browse through the location and save the file as desired.
I have tried some thing but
1. The dialog box goes behind the application
2. When run, dialogbox opens 3 times, means it executes 3 times
REPLY TO THE POST
protected void btnNewFile_Click(object sender, EventArgs e)
{
StreamWriter sw = null;
try
{
SaveFileDialog sdlg = new SaveFileDialog();
DialogResult result = sdlg.ShowDialog();
sdlg.InitialDirectory = @"C:\";
sdlg.AddExtension = true;
sdlg.CheckPathExists = true;
sdlg.CreatePrompt = false;
sdlg.OverwritePrompt = true;
sdlg.ValidateNames = true;
sdlg.ShowHelp = true;
sdlg.DefaultExt = "txt";
string file = sdlg.FileName.ToString();
string data = txtNewFile.Text;
if (sdlg.ShowDialog() == DialogResult.OK)
{
sw.WriteLine(txtNewFile.Text);
sw.Close();
}
if (sdlg.ShowDialog() == DialogResult.Cancel)
{ sw.Dispose(); }
}
catch
{ }
finally
{
if (sw != null)
{
sw.Close();
}
}
}
private void Save(string file, string data)
{
StreamWriter writer = new StreamWriter(file);
SaveFileDialog sdlg1 = new SaveFileDialog();
try
{
if (sdlg1.ShowDialog() == DialogResult.OK)
{
writer.Write(data);
writer.Close();
}
else
writer.Dispose();
}
catch (Exception xp)
{
MessageBox.Show(xp.Message);
}
finally
{
if (writer != null)
{
writer.Close();
}
}
}
I have tried this.
Best Answer
The SaveFileDialog is a windows forms control, it doesn't work on a website.
A browser will display the "What do you want to do with this file" dialog whenever a server sends it a stream it can't handle by default - unfortunately, most browsers can handle text streams, so will just display them to the user.
But something like this should get you going:
But as I said, most browsers can cope with plain text, so you might need to lie to the browser and pass in a application type, but then that might limit the usefulness of the download on some machines.