简体   繁体   English

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

[英]Download file from web and then save with a save file dialog box?

How can I download a file, then save it to wherever I want?如何下载文件,然后将其保存到我想要的任何位置? I am using Windows Form, Web Application.我正在使用 Windows 窗体、Web 应用程序。

I know I can download it with this code:我知道我可以使用以下代码下载它:

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

But I want a save box like when you press CTRL+S.但我想要一个保存框,就像你按 CTRL+S 一样。

You can use SaveFileDialog class.您可以使用SaveFileDialog类。 Example:例子:

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

will show next dialog and if you search for next folder将显示下一个对话框,如果您搜索下一个文件夹在此处输入图片说明

application will print:应用程序将打印:

writing to: C:\Temp\archiveName.rar

This will work and open the file download popup.这将起作用并打开文件下载弹出窗口。

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

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

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