How to save a web page as HTML or MHT | ||||||||||||||||
Here's how to save a web page displayed inside a WebBrowser (TWebBrowser component) as a raw HTML file or into a single (MHT) file (MHTML format: web archive - single file). | ||||||||||||||||
When working with Delphi, the TWebBrowser component allows you to create a customized Web browsing application or to add Internet, file and network browsing, document viewing, and data downloading capabilities to your applications. File ... Save As; or how to save a web page from TWebBrowser
The TWebBrowser component (located on the "Internet" page of the Component Palette) provides access to the Web browser functionality from your Delphi applications. In general, you'll want to enable saving of a web page displayed inside a WebBrowser as a HTML file to a disk. Saving a web page as a raw HTMLIf you only want to save a web page as a raw HTML you would select "Web Page, HTML only (*.htm, *.html)". It will simply save the current page's source HTML to your drive intact. This action will NOT save the graphics from the page or any other files used within the page, which means that if you loaded the file back from the local disk, you would see broken image links.Here's how to save a web page as raw HTML using Delphi code:
Usage sample:
Note 1: the IPersistStreamInit and IStream interfaces are declared inside the ActiveX unit. MHT : Web archive - single fileWhen you save a Web page as "Web archive, single file (*.mht)" the web document gets saved in the Multipurpose Internet Mail Extension HTML (MHTML) format with a .mht file extension. All relative links in the Web page are remapped and the embedded content is included in the .mht file, rather than being saved in a separate folder (as the case is with "Web Page, complete (*.htm, *.html)").MHTML enables you to send and receive Web pages and other HTML documents using e-mail programs such as Microsoft Outlook, and Microsoft Outlook Express; or even your custom Delphi email sending solutions. MHTML enables you to embed images directly into the body of your e-mail messages rather than attaching them to the message. Here's how to save a web page as a single file (mht format) using Delphi code:
Sample usage:
Note 1: The _Stream class is defined in ADODB_TLB unit that you probably already have created. The IMessage and IConfiguration interfaces code from cdosys.dll library. CDO stands for Collaboration Data Objects - object libraries designed to enable SMTP Messaging.
Note that you could rewrite the WB_SaveAs_MHT procedure to accept an URL string (not TWebBrowser) to be able to save a web page directly - no need to use the WebBrowser component. The URL from WebBrowser is retrieved using the WB.LocationURL property. |
How to save a web page as HTML or MHT
最新推荐文章于 2021-06-16 18:59:21 发布