简体   繁体   中英

Automatically select save in dialog box c#

I am building an application in C# that opens a link to a .jpg using the web browser (to download it).

Some browsers will automatically download and with some others a dialog box will open. On the default webBrowser1 it shows a dialog box open save cancel . Is there a way for my application to automatically select save ?


Read on for more info on the project:

I have 3 web browsers within a form.

webBrowser1 opens a page on form load and has a button which:

  • Searches for specific links on page using regex. And then saves them to a public static array => links[] .

  • Opens webBrowser2

  • Hides the button

  • Hides webBrowser1

webBrowser2

  1. On load opens the first link => links[0]

  2. On webBrowser2 load checks if it contains regex2

  3. If true find another link (the .jpg link) with regex3 => second_links[] (can only have none or 1)

    • If no link returns to step 1
  4. Opens link second_links[0] in webBrowser3 . (This bit can cause errors because it then goes back to step 1 before webBrowser3 has saved the .jpg . Any ideas on how to to get around that?)

Here's an example of how to use the HttpClient to download a jpeg file. Note that this assumes VS2012 and uses async/await. You'll need to reference System.Net.Http in your project for this to build.

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

As suggested in the comments, download the file directly.

For example:

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

clientResponse is a byte[] that would contain the image.

To write to disk:

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

You could use the following for simplicity:

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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM