从Web下载文件,然后使用保存文件对话框保存?

时间:2013-11-14 07:48:55

标签: c# download save

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

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

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

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

2 个答案:

答案 0 :(得分:1)

您可以使用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);
}

将显示下一个对话框,如果您搜索下一个文件夹 enter image description here

应用程序将打印:

writing to: C:\Temp\archiveName.rar

答案 1 :(得分:1)

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

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