繁体   English   中英

另存为对话框,使用asp.net将文本框内容保存到新文件

[英]Save as DialogBox to save textbox content to a newfile using asp.net

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

我尝试了一些东西,但是
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();
        }
    }
}

我已经试过了。

您可以从以下链接中了解点子:
http://codingforums.com/showthread.php?t=90278

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

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

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

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

您应该设置HTTP标头

Content-Disposition: attachment; filename=somefilename.txt

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

        DialogResult result = sdlg.ShowDialog();

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

        }

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的缓存规则)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM