[英]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). 我正在C#中构建一个应用程序,该应用程序使用Web浏览器(下载)打开到
.jpg
的链接。
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 . 在默认的
webBrowser1
它显示一个打开 保存 取消对话框。 Is there a way for my application to automatically select save ? 我的应用程序可以自动选择保存吗?
I have 3 web browsers within a form. 我在表单中有3个Web浏览器。
webBrowser1 opens a page on form load and has a button which: webBrowser1在表单加载时打开一个页面,并具有一个按钮,该按钮:
Searches for specific links on page using regex. 使用正则表达式搜索页面上的特定链接。 And then saves them to a public static array =>
links[]
. 然后将它们保存到公共静态数组=>
links[]
。
Opens webBrowser2 打开webBrowser2
Hides the button 隐藏按钮
Hides webBrowser1 隐藏webBrowser1
webBrowser2 webBrowser2
On load opens the first link => links[0]
加载时打开第一个链接=>
links[0]
On webBrowser2 load checks if it contains regex2 在webBrowser2上加载检查是否包含regex2
If true find another link (the .jpg
link) with regex3 => second_links[]
(can only have none or 1) 如果为true,则使用regex3 =>
second_links[]
(只能是一个或一个)找到另一个链接( .jpg
链接)。
Opens link second_links[0]
in webBrowser3 . 在webBrowser3中打开链接
second_links[0]
。 (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?) (此位可能会导致错误,因为它会在webBrowser3保存
.jpg
之前返回步骤1。有关如何解决该问题的任何想法?)
Here's an example of how to use the HttpClient to download a jpeg file. 这是一个如何使用HttpClient下载jpeg文件的示例。 Note that this assumes VS2012 and uses async/await.
请注意,这假设使用VS2012并使用async / await。 You'll need to reference System.Net.Http in your project for this to build.
您需要在项目中引用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();
}
}
}
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. clientResponse
是将包含图像的byte[]
。
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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.