在asp.net中保存为对话框

我有一些字符串forms的数据。 我想将此数据写入文件并将文件保存到指定的路径。 通过在按钮单击上打开另存为对话框来指定路径。 怎么能实现?

最初使用此代码将文件保存到服务器中

string getnote = txtdisplay.Text.Trim(); String filepath = Server.MapPath(@"img\new1.txt"); System.IO.FileStream ab = new System.IO.FileStream(filepath, System.IO.FileMode.Create); System.IO.StreamWriter Write101 = new System.IO.StreamWriter(ab); Write101.WriteLine(getnote); Write101.Close(); Response.ClearContent(); 

从服务器获取文件作为附件。使用以下代码保存为对话框以下载或保存文件。 该文件默认保存在下载文件夹中。 要保存到指定位置,请更改浏览器设置。

 Response.ContentType = "text"; Response.AppendHeader("Content-Disposition", "attachment; filename=new1.txt"); Response.TransmitFile(Server.MapPath("~/img/new1.txt")); Response.End(); 
 Response.ContentType = "application/octet-stream" (or content type of your file). Response.AppendHeader("content-disposition", "attachment;filename=" & strFileName) 

ASP.NET中没有“另存为”对话框。

请记住,您的ASP.NET应用程序正在用户计算机上的浏览器中运行。 您无权访问用户的文件系统,包括“另存为”对话框。

但是,如果您向用户发送文件作为附件,大多数浏览器将显示一个对话框,询问用户是保存文件还是打开文件。 也许用户会选择保存它。 这就是凤凰的例子。

您可以使用LinkBut​​ton(或常规链接)并使url指向处理程序 (ASHX),该处理程序检索数据并将内容处置设置为附件的响应发回。 将数据写入响应。 您还需要在响应中设置一些其他标头 – 例如内容类型和长度。 这将为文档(文件)提供一个常规链接,该链接可能在将来被加入书签(如果是常规链接),以便可以再次检索它。 您需要在查询字符串中传递足够的数据,以便能够识别要下载的数据。

如果我的用户正确,在这里 –

 saveFileDialog1.DefaultExt = "*.file"; saveFileDialog1.Filter = "File|*.file|Other File|*.OFile|"; if (saveFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK && saveFileDialog1.FileName.Length > 0) { WebClient wc = new WebClient(); wc.DownloadFile("http://www.exaple.com/exaplefile", saveFileDialog1.FileName);; }