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

问题描述:

我希望用户在给定的文本框中输入文本,并在单击createNewFile按钮时弹出SaveAs对话框,用户应浏览位置并根据需要保存文件。保存为对话框以将文本框内容保存到使用asp.net的新文件

我尝试过的一些事情,但
1.对话框去
2.在运行时,对话框打开3倍的应用程序背后,意味着它执行3次

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

我试过这个。

+0

发布您尝试过的内容,否则我们可能会建议您已尝试过的某些内容 – ChrisF 2009-11-17 09:11:15

+0

您希望在客户端或服务器上保存文件的位置? – Murph 2009-11-17 09:50:38

+0

我想为服务器做,但因为Iam初学者, 我想为本地。 – user195114 2009-11-17 10:56:19

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

转换在C#和更新编写文本框的文本。

+0

感谢您的回复, 但我没有得到它的权利。 – user195114 2009-11-17 11:25:15

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) 
     { 

     } 

结果onlyas没有另存为对话框中的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并返回给用户。此时,浏览器拦截结果输出并询问用户如何处理该特定文件。

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

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

+0

非常感谢,这真的很有帮助,比解释真的非常有价值。 Thanx再次 – user195114 2009-11-18 12:21:37

+0

没问题,我的荣幸:) – ntze 2009-11-18 14:16:43