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

六眼飞鱼酱① 提交于 2019-12-23 20:33:16

问题


How can I download a file, then save it to wherever I want? I am using Windows Form, Web Application.

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.


回答1:


You can use SaveFileDialog class. 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



回答2:


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


来源:https://stackoverflow.com/questions/19972266/download-file-from-web-and-then-save-with-a-save-file-dialog-box

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!