4

如何下载文件,然后将其保存到我想要的任何地方?我正在使用 Windows 窗体、Web 应用程序。

我知道我可以使用以下代码下载它:

WebClient wClient = new WebClient();
wClient.DownloadFile("WebLinkHere", @"C:\File.txt");

但我想要一个保存框,就像你按 CTRL+S 时一样。

4

2 回答 2

5

您可以使用SaveFileDialog类。例子:

var dialog = new SaveFileDialog();
dialog.Filter = "Archive (*.rar)|*.rar";

var result = dialog.ShowDialog(); //shows save file dialog
if(result == DialogResult.OK)
{
    Console.WriteLine ("writing to: " + dialog.FileName); //prints the file to save

    var wClient = new WebClient();
    wClient.DownloadFile("WebLinkHere", dialog.FileName);
}

将显示下一个对话框,如果您搜索下一个文件夹 在此处输入图像描述

应用程序将打印:

writing to: C:\Temp\archiveName.rar
于 2013-11-14T07:54:09.933 回答
2

这将起作用并打开文件下载弹出窗口。

String FileName = "FileName.xls";
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", FileName));
            Response.ContentType = "application/ms-excel";
            StringWriter stringWriter = new StringWriter();
            HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);
            Response.Write(stringWriter.ToString());
            Response.End();
于 2014-01-27T20:28:32.727 回答