如何在不使用“文件下载”对话框的情况下使用 WebBrowser 控件下载文件?
How can I download a file using a WebBrowser control without using the File Download dialog box?
在一个WPF项目中,我使用方法
webBrowser.Navigate(strUrl);
从服务器获取一张PNG图片。
出现以下对话框:
我怎样才能无缝下载图片(没有对话框)?
您不需要为此使用浏览器控件。
尝试使用 DownloadFileAsync()
这是一个完整的示例。 (根据需要更改路径。)
private void Button_Click(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.DownloadFileAsync(new Uri("https://www.example.com/filepath"), @"C:\Users\currentuser\Desktop\Test.png");
client.DownloadFileCompleted += Client_DownloadFileCompleted;
client.DownloadProgressChanged += Client_DownloadProgressChanged;
}
private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
TBStatus.Text = e.ProgressPercentage + "% " + e.BytesReceived + " of " + e.TotalBytesToReceive + " received.";
}
private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MessageBox.Show("Download Completed");
}
您可以像这样使用默认应用程序打开下载的文件:
System.Diagnostics.Process.Start(@"C:\Users\currentuser\Desktop\Test.png");
编辑:
如果您的目标只是显示 png,您可以将其下载到流中,然后在 image control.
中显示
完整的示例。
WebClient wc = new WebClient();
MemoryStream stream = new MemoryStream(wc.DownloadData("https://www.dropbox.com/s/l3maq8j3yzciedw/App%20in%205%20minutes.PNG?raw=1"));
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream);
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
image1.Source = bi;
这是异步版本。
private void Button_Click(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.DownloadDataAsync(new Uri("https://www.dropbox.com/s/l3maq8j3yzciedw/App%20in%205%20minutes.PNG?raw=1"));
wc.DownloadDataCompleted += Wc_DownloadDataCompleted;
}
private void Wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
MemoryStream stream = new MemoryStream((byte[])e.Result);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream);
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
image1.Source = bi;
}
在一个WPF项目中,我使用方法
webBrowser.Navigate(strUrl);
从服务器获取一张PNG图片。
出现以下对话框:
我怎样才能无缝下载图片(没有对话框)?
您不需要为此使用浏览器控件。
尝试使用 DownloadFileAsync()
这是一个完整的示例。 (根据需要更改路径。)
private void Button_Click(object sender, RoutedEventArgs e)
{
WebClient client = new WebClient();
client.DownloadFileAsync(new Uri("https://www.example.com/filepath"), @"C:\Users\currentuser\Desktop\Test.png");
client.DownloadFileCompleted += Client_DownloadFileCompleted;
client.DownloadProgressChanged += Client_DownloadProgressChanged;
}
private void Client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar.Value = e.ProgressPercentage;
TBStatus.Text = e.ProgressPercentage + "% " + e.BytesReceived + " of " + e.TotalBytesToReceive + " received.";
}
private void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MessageBox.Show("Download Completed");
}
您可以像这样使用默认应用程序打开下载的文件:
System.Diagnostics.Process.Start(@"C:\Users\currentuser\Desktop\Test.png");
编辑:
如果您的目标只是显示 png,您可以将其下载到流中,然后在 image control.
中显示完整的示例。
WebClient wc = new WebClient();
MemoryStream stream = new MemoryStream(wc.DownloadData("https://www.dropbox.com/s/l3maq8j3yzciedw/App%20in%205%20minutes.PNG?raw=1"));
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream);
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
image1.Source = bi;
这是异步版本。
private void Button_Click(object sender, RoutedEventArgs e)
{
WebClient wc = new WebClient();
wc.DownloadDataAsync(new Uri("https://www.dropbox.com/s/l3maq8j3yzciedw/App%20in%205%20minutes.PNG?raw=1"));
wc.DownloadDataCompleted += Wc_DownloadDataCompleted;
}
private void Wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
MemoryStream stream = new MemoryStream((byte[])e.Result);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(stream);
bmp.Save(stream, System.Drawing.Imaging.ImageFormat.Png);
stream.Position = 0;
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.StreamSource = stream;
bi.EndInit();
image1.Source = bi;
}