Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
在本文中,您将看到一个可以用来从 URL 保存文件的 C# 示例。从 URL 下载文件时,您可以根据自己的需要访问、共享、保存和使用网络上的文件。保存文件有几个原因:
为了从 URL 保存文件,您需要知道它的 URL 并拥有适合其协议的网络操作处理程序。 Aspose.HTML for .NET 库 为您提供了处理不同协议的 URL 的便捷功能。要使用它,只需创建一个空的 HTML 文档并调用网络请求处理程序即可,如下面的 C# 示例所示:
url) 构造函数创建 RequestMessage 类的新实例,该实例代表 HTTP 请求消息。url 参数会传递给构造函数,指定发送请求的 URL。request) 方法发送请求。将对响应进行检查,以确保发送成功。File.WriteAllBytes() 方法将文件保存到本地文件系统。让我们看看如何使用 Aspose.HTML C# 库从 URL 保存文件:
1// Download file from URL using C#
2
3// Create a blank document; it is required to access the network operations functionality
4using HTMLDocument document = new HTMLDocument();
5
6// Create a URL with the path to the resource you want to download
7Url url = new Url("https://docs.aspose.com/html/net/message-handlers/message-handlers.png");
8
9// Create a file request message
10using RequestMessage request = new RequestMessage(url);
11
12// Download file from URL
13using ResponseMessage response = document.Context.Network.Send(request);
14
15// Check whether response is successful
16if (response.IsSuccess)
17{
18 // Save file to a local file system
19 File.WriteAllBytes(Path.Combine(OutputDir, url.Pathname.Split('/').Last()), response.Content.ReadAsByteArray());
20}您可以使用 GitHub 中的完整 C# 示例从 URL 下载文件。
Aspose.HTML 提供 HTML 网络应用程序,它是免费转换器、合并器、搜索引擎优化工具、HTML 代码生成器、URL 工具等的在线集合。这些应用程序可在任何装有网络浏览器的操作系统上运行,无需安装任何其他软件。无论你身在何处,都能轻松转换、合并、编码、生成 HTML 代码,从网络中提取数据,或从搜索引擎优化的角度分析网页。使用我们的 HTML 网络应用程序集来处理您的日常事务,让您的工作流程天衣无缝!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.