【问题标题】:Automatically select save in dialog box c#在对话框c#中自动选择保存
【发布时间】:2014-02-20 01:31:53
【问题描述】:

我正在用 C# 构建一个应用程序,它使用 Web 浏览器打开一个指向 .jpg 的链接(以下载它)。

有些浏览器会自动下载,而有些浏览器会打开一个对话框。默认webBrowser1 会显示一个对话框open save cancel。有没有办法让我的应用程序自动选择 save


继续阅读以了解有关该项目的更多信息:

我在一个表单中有 3 个网络浏览器。

webBrowser1 在表单加载时打开一个页面,并有一个按钮:

  • 使用正则表达式搜索页面上的特定链接。然后保存它们 到公共静态数组 => links[].

  • 打开 webBrowser2

  • 隐藏按钮

  • 隐藏 webBrowser1

webBrowser2

  1. 加载时打开第一个链接 => links[0]

  2. 在 webBrowser2 上加载检查它是否包含 regex2

  3. 如果为 true,则使用 regex3 => second_links[] 查找另一个链接(.jpg 链接)(只能没有或 1)

    • 如果没有链接返回步骤 1
  4. webBrowser3 中打开链接 second_links[0]。 (此位可能会导致错误,因为它会在 webBrowser3 保存 .jpg 之前返回到第 1 步。关于如何解决这个问题的任何想法?)

【问题讨论】:

  • 您使用浏览器而不是 HttpClient 是否有原因?届时您将拥有完全的控制权。
  • 我是 C# 新手。这是更好的选择吗?
  • 更好的解决方案。
  • @PeterR 我的意思是初学者?看起来很复杂。
  • @PeterR 哦,你的意思不是说使用 HttpClient 完成所有的事情吗?只是下载。抓住你!

标签: c# regex dialog


【解决方案1】:

这是一个如何使用 HttpClient 下载 jpeg 文件的示例。请注意,这假定 VS2012 并使用 async/await。您需要在项目中引用 System.Net.Http 才能构建它。

using System;
using System.Diagnostics;
using System.IO;
using System.Net.Http;

namespace DownloadSample
{
    class Program
    {

        static async void RunClient(string address)
        {
            HttpClient client = new HttpClient();

            // Send asynchronous request
            HttpResponseMessage response = await client.GetAsync(address);

            // Check that response was successful or throw exception
            response.EnsureSuccessStatusCode();

            // Read response asynchronously and save asynchronously to file
            using (FileStream fileStream = new FileStream("c:\\temp\\logo.jpg", FileMode.Create, FileAccess.Write, FileShare.None))
            {
                await response.Content.CopyToAsync(fileStream);
            }
        }

        static void Main(string[] args)
        {
            string microsoft_logo = "http://c.s-microsoft.com/en-au/CMSImages/mslogo.png?version=856673f8-e6be-0476-6669-d5bf2300391d";
            RunClient(microsoft_logo); //"http://some.domain.com/resource/file.jpg");

            Console.WriteLine("Check download folder");
            Console.ReadLine();
        }
    }
}

【讨论】:

    【解决方案2】:

    按照 cmets 中的建议,直接下载文件。

    例如:

    var client = new HttpClient();
    var clientResponse = await client.GetByteArrayAsync(imageUri);
    

    clientResponse 是一个包含图像的byte[]

    写入磁盘:

    using (var fs = new FileStream("path_to_file", FileMode.Create))
    {
        fs.Write(clientResponse, 0, clientResponse.Length);
    }
    

    【讨论】:

    • 抱歉下载到哪里去了?你会怎么说把它放在你的下载文件夹中?
    • 您获取字节,并将它们写入文件。
    【解决方案3】:

    为简单起见,您可以使用以下内容:

    var filename = @"C:\image.png";
    var url = @"http://www.somedomain.com/image.png";
    
    using (var client = new System.Net.WebClient())
    {
        client.DownloadFile(url, filename);
    }
    
    using (var image = System.Drawing.Image.FromFile(filename))
    {
        // Do something with image.
    }
    

    【讨论】:

      猜你喜欢
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多