0

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.

vrajs5
  • 4,066
  • 1
  • 27
  • 44
user195114
  • 31
  • 2
  • 9

4 Answers4

0

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:

protected void btnNewFile_Click(object sender, EventArgs e)
{
   // Clear the response buffer:
   Response.Clear();

   // Set the output to plain text:
   Response.ContentType = "text/plain";

   // Send the contents of the textbox to the output stream:
   Response.Write(txtNewFile.Text);

   // End the response so we don't get anything else sent (page furniture etc):
   Response.End();
}

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.

Zhaph - Ben Duguid
  • 26,785
  • 5
  • 80
  • 117
0

As others have said, you can't use the SaveFileDialog. If you do, it will only be visible on the server, and the user can never see it. You can only see it because in your case the server and the client happen to be the same.

You should set the HTTP header

Content-Disposition: attachment; filename=somefilename.txt
erikkallen
  • 33,800
  • 13
  • 85
  • 120
0

I assume you are trying this in Winforms env. The issue here is you are issuing three calls to .ShowDialog in your code which pops the dialog three times. You just need to call ShowDialog once and then save and use the result onlyas below

        DialogResult result = sdlg.ShowDialog();

        if (result == DialogResult.OK)            
        {                
            sw.WriteLine(data);                
            sw.Close();            
        }
        else if (result == DialogResult.Cancel)
        { 

        }
MSIL
  • 2,671
  • 5
  • 24
  • 25
0

There is no SaveAs Dialogbox in ASP.NET. But you can force your application to generate a file on the server and have it sent back to the user.

string userProvidedText               = uiTextBox.Text; // this is your textbox
byte[] userProvidedTextAsBytes        = null;

if (!string.IsNullOrEmpty(userProvidedText )) {
  System.Text.ASCIIEncoding encoding  = new System.Text.ASCIIEncoding();
  userProvidedTextAsBytes             = encoding.GetBytes(userProvidedText);
}

Response.AppendHeader("Content-Disposition", "attachment; filename=YourFileName.html");
Response.ContentType = "text/HTML";
Response.BinaryWrite(userProvidedTextAsBytes);
Response.End();

When this code runs, the applications generates "on the fly" YourFileName.html and returns it to the user. At this point the Browser intercepts the resulting output and ask the user what to do with that specific file.

alt text http://www.cyphersec.com/wp-content/uploads/2009/04/output1.png

Note: Use Response.TransmitFile() if you want to serve previously stored files. The reason behind it is that TransmitFile is very efficent because it basically offloads the file streaming to IIS including potentially causing the file to get cached in the Kernel (base on IIS’s caching rules).

ntze
  • 126
  • 1
  • 5
  • Thanks a lot, It was really very helpful, more than the explanation is really very valuable. Thanx Once again – user195114 Nov 18 '09 at 12:21