如何在不使用文件下载对话框的情况下使用WebBrowser控件下载文件?

4
在 WPF 项目中,我使用了该方法。
webBrowser.Navigate(strUrl);

从服务器获取PNG图片。
出现以下对话框:

File Download dialog screenshot

如何无缝下载图片(无需对话框)?

2
由于这是一个WPF应用程序,不要通过Web浏览器保存文件。相反,您可以使用FileStream写入驱动器位置。 - Henrik Bøgelund Lavstsen
如果文件在远程服务器上,您可以请求下载流,然后将其移动到驱动器的文件流中。目前,您只能使用浏览器打开图片,浏览器将执行其默认任务。 - Henrik Bøgelund Lavstsen
你需要做一些小技巧,查看这个帖子 - Pavel Anikhouski
2
这是服务器信息(“Content-Disposition”标头可以设置为inlineattachment)和HTTP用户代理(此处为WPF中嵌入的IE)配置的组合。请参阅BrowserFlags注册表设置https://support.microsoft.com/zh-cn/help/982995/a-new-application-window-opens-when-you-try-to-view-an-office-2010-or和https://learn.microsoft.com/zh-cn/windows/desktop/api/shlwapi/ne-shlwapi-filetypeattributeflags。 - Simon Mourier
好的,我已经添加了一个示例,使用图像控件。 - Neil B
显示剩余4条评论
1个回答

3

您不需要使用浏览器控件来实现此功能。

尝试使用 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;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接