保存为DialogBox以使用asp.net将文本框内容保存到新文件

时间:2009-11-17 08:51:10

标签: c# asp.net savefiledialog

我希望用户在给定的文本框中键入文本,然后单击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();
        }
    }
}

我试过这个。

5 个答案:

答案 0 :(得分:0)

你可以从这个链接中获取想法:
http://codingforums.com/showthread.php?t=90278

在c#中转换它并更新以编写文本框文本。

答案 1 :(得分:0)

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

当服务器向其发送默认无法处理的流时,浏览器将显示“您要对此文件做什么”对话框 - 遗憾的是,大多数浏览器都可以处理文本流,因此只显示它们用户。

但是这样的事情会让你前进:

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

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

答案 2 :(得分:0)

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

您应该设置HTTP标头

Content-Disposition: attachment; filename=somefilename.txt

答案 3 :(得分:0)

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

        DialogResult result = sdlg.ShowDialog();

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

        }

答案 4 :(得分:0)

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

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并将其返回给用户。此时,浏览器拦截结果输出并询问用户如何处理该特定文件。

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

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

相关问题