如何使用弹出窗口询问保存文件的位置,在 C# 中下载文件
How to download a file in c# with a popup asking where to save file
WebClient webClient = new WebClient();
webClient.DownloadFile(pdfFilePath, @"D:\DownloadPastPapers.pdf");
我正在下载直接下载到指定路径的pdf文件,但我想打开一个弹出窗口询问保存位置(如所有正常网站在下载时显示的那样)
它是一个 webfroms asp.net 应用程序
您可以先弹出一个SaveFileDialog
询问保存路径。
然后在您的 DownloadFile()
中使用此路径
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Filter = "Your extension here (*.EXT)|*.ext|All Files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 1;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Console.WriteLine(saveFileDialog1.FileName);//Do what you want here
WebClient webClient = new WebClient();
webClient.DownloadFile(pdfFilePath, saveFileDialog1.FileName");
}
private string SelectDestinationFile()
{
var dialog = new SaveFileDialog()
{
Title = "Select output file"
//--Filter can also be defined here
};
return dialog.ShowDialog() == true ? dialog.FileName : null;
}
稍后
private void DownloadFile(string url)
{
var filePath = SelectDestinationFile();
if(string.IsNullOrWhiteSpace(filePath))
throw new InvalidOperationException("invalid file path");
using (var client = new WebClient())
client.DownloadFile(url, filePath);
}
希望对你有所帮助
P.S. 这适用于 WPF 应用程序。
pdfFilePath = pdfFilePath + "/DownloadPastPapers.pdf";
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
Response.TransmitFile(pdfFilePath);
Response.End();
WebClient webClient = new WebClient();
webClient.DownloadFile(pdfFilePath, @"D:\DownloadPastPapers.pdf");
我正在下载直接下载到指定路径的pdf文件,但我想打开一个弹出窗口询问保存位置(如所有正常网站在下载时显示的那样) 它是一个 webfroms asp.net 应用程序
您可以先弹出一个SaveFileDialog
询问保存路径。
然后在您的 DownloadFile()
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.Filter = "Your extension here (*.EXT)|*.ext|All Files (*.*)|*.*" ;
saveFileDialog1.FilterIndex = 1;
if(saveFileDialog1.ShowDialog() == DialogResult.OK)
{
Console.WriteLine(saveFileDialog1.FileName);//Do what you want here
WebClient webClient = new WebClient();
webClient.DownloadFile(pdfFilePath, saveFileDialog1.FileName");
}
private string SelectDestinationFile()
{
var dialog = new SaveFileDialog()
{
Title = "Select output file"
//--Filter can also be defined here
};
return dialog.ShowDialog() == true ? dialog.FileName : null;
}
稍后
private void DownloadFile(string url)
{
var filePath = SelectDestinationFile();
if(string.IsNullOrWhiteSpace(filePath))
throw new InvalidOperationException("invalid file path");
using (var client = new WebClient())
client.DownloadFile(url, filePath);
}
希望对你有所帮助
P.S. 这适用于 WPF 应用程序。
pdfFilePath = pdfFilePath + "/DownloadPastPapers.pdf";
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=foo.pdf");
Response.TransmitFile(pdfFilePath);
Response.End();