0

我希望用户在给定的文本框中键入他们的文本,然后单击 createNewFile 按钮,应弹出一个 SaveAs 对话框,用户应浏览该位置并根据需要保存文件。

我已经尝试了一些东西,但是
1. 对话框在应用程序后面
2. 运行时,对话框打开 3 次,意味着它执行了 3 次

回复帖子

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();
        }
    }
}

我试过这个。

4

4 回答 4

0

我假设您正在 Winforms 环境中尝试此操作。这里的问题是您在代码中发出三个对 .ShowDialog 的调用,这会弹出对话框三次。您只需要调用 ShowDialog 一次,然后保存并使用结果,如下所示

        DialogResult result = sdlg.ShowDialog();

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

        }
于 2009-11-17T11:56:05.803 回答
0

ASP.NET 中没有另存为对话框。但是您可以强制您的应用程序在服务器上生成文件并将其发送回用户。

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();

当此代码运行时,应用程序会“即时”生成 YourFileName.html 并将其返回给用户。此时浏览器截取结果输出并询问用户如何处理该特定文件。

替代文字 http://www.cyphersec.com/wp-content/uploads/2009/04/output1.png

注意:如果要提供以前存储的文件,请使用Response.TransmitFile() 。其背后的原因是 TransmitFile 非常有效,因为它基本上将文件流式传输到 IIS,包括可能导致文件缓存在内核中(基于 IIS 的缓存规则)。

于 2009-11-17T12:15:25.903 回答
0

SaveFileDialog是一个窗体控件,它在网站上不起作用。

每当服务器向它发送默认情况下无法处理的流时,浏览器都会显示“你想用这个文件做什么”对话框 - 不幸的是,大多数浏览器都可以处理文本流,因此只会将它们显示给用户。

但是这样的事情应该让你继续前进:

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();
}

但正如我所说,大多数浏览器都可以处理纯文本,因此您可能需要对浏览器撒谎并传入应用程序类型,但这可能会限制在某些机器上下载的有用性。

于 2009-11-17T11:12:54.313 回答
0

正如其他人所说,您不能使用 SaveFileDialog。如果这样做,它只会在服务器上可见,而用户永远看不到它。您只能看到它,因为在您的情况下,服务器和客户端恰好是相同的。

您应该设置 HTTP 标头

Content-Disposition: attachment; filename=somefilename.txt
于 2009-11-17T11:25:53.773 回答