Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
MHTML 将普通 HTML 与图像、动画、音频等外部资源合并为一个文件,文件扩展名为 .mht。 MHTML 使用 MIME 电子邮件协议将项目合并为一个网页存档文件。有了 MHTML,在线网页的存档变得更加容易,也不那么杂乱。
本文将介绍如何将 HTML 转换为 MHTML 以及如何使用 MHTMLSaveOptions。
您可以使用 Aspose.HTML for .NET API 将 HTML 实时转换为 MHTML。首先,从本地硬盘加载 HTML 文件,然后运行示例。在本示例中,默认设置了保存选项。您将立即以单独文件的形式收到转换结果。
如果您想通过编程将 HTML 转换为 MHTML,请参阅以下 C# 代码示例。
Converter 类的静态方法主要用作将 HTML 代码转换为各种格式的最简单方法。只需一行代码,您就可以在 C# 应用程序中将 HTML 转换为 MHTML!
1// Convert HTML to MHTML using C#
2
3// Invoke the ConvertHTML() method to convert HTML to MHTML
4Converter.ConvertHTML(@"<h1>Hellow, Word!</h1>", ".", new MHTMLSaveOptions(), Path.Combine(OutputDir, "convert-with-single-line.mht"));使用 ConvertHTML() 方法将文件转换为另一种格式是一系列操作,其中包括文档加载和保存。下一个示例将逐行解释如何将 HTML 转换为 MHTML:
1// Convert HTML to MHTML in C#
2
3// Prepare a path to a source HTML file
4string documentPath = Path.Combine(DataDir, "drawing.html");
5
6// Prepare a path to save the converted file
7string savePath = Path.Combine(OutputDir, "drawing-output.mht");
8
9// Initialize an HTML document from the file
10using HTMLDocument document = new HTMLDocument(documentPath);
11
12// Initialize MHTMLSaveOptions
13MHTMLSaveOptions options = new MHTMLSaveOptions();
14
15// Convert HTML to MHTML
16Converter.ConvertHTML(document, options, savePath);Aspose.HTML 允许使用默认或自定义保存选项将 HTML 转换为 MHTML。使用 MHTMLSaveOptions 可以自定义渲染过程;可以指定 PageUrlRestriction、ResourceUrlRestriction、MaxHandlingDepth 等。
| Property | Description |
|---|---|
| PageUrlRestriction | This property gets or sets restrictions applied to URLs of handled pages. The default value is RootAndSubFolders. |
| ResourceUrlRestriction | Gets or sets restrictions applied to URLs of handled resources such as CSS, js, images, etc. The default is SameHost. |
| MaxHandlingDepth | Gets or sets the maximum depth of pages that will be handled. |
要了解有关 MHTMLSaveOptions 的更多信息,请阅读 微调转换器 一文。
要通过指定 MHTMLSaveOptions 将 HTML 转换为 MHTML,应遵循以下几个步骤:
下面的示例展示了如何使用 MHTMLSaveOptions 并创建带有自定义保存选项的 MHTML 文件:
1// Convert HTML to MHTML with custom settings using C#
2
3// Prepare HTML code with a link to another file and save it to the file as 'document.html'
4string code = "<span>Hello, World!!</span> " +
5 "<a href='document2.html'>click</a>";
6File.WriteAllText("document.html", code);
7
8// Prepare HTML code and save it to the file as 'document2.html'
9code = @"<span>Hello, World!!</span>";
10File.WriteAllText("document2.html", code);
11
12string savePath = Path.Combine(OutputDir, "output-options.mht");
13
14// Change the value of the resource linking depth to 1 in order to convert document with directly linked resources
15MHTMLSaveOptions options = new MHTMLSaveOptions()
16{
17 ResourceHandlingOptions =
18 {
19 MaxHandlingDepth = 1
20 }
21};
22
23// Convert HTML to MHTML
24Converter.ConvertHTML("document.html", options, savePath);MHTMLSaveOptions() 构造函数初始化 MHTMLSaveOptions 类的实例,并将其传递给 ConvertHTML() 方法。ConvertHTML() 方法接收 document, options, 输出文件路径 savePath 并执行转换操作。MHTMLSaveOptions 类提供了大量属性,可让您全面控制各种参数,并改进 HTML 与 MHTML 格式的转换过程。
在上面的示例中,我们使用的属性 MaxHandlingDepth = 1 表示只处理保存文档中直接引用的页面。
Aspose.HTML 提供免费的在线 HTML 到 MHTML 转换器,可将 HTML 高质量、方便快捷地转换为 MHTML。只需上传、转换您的文件,几秒钟就能得到结果!
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.