Sunday, May 21, 2006

Saving TWebbrowser content with IPersistFile

With TWebbrowser it is easy to navigate URL's and files on your local disk using the Navigate method.
As discussed in this blogpost TWebbrowser implements a IHTMLDocument2 interface object which can be used to edit the document in WYSIWYG way.

Saving the changes can be done in a few different ways, I will discuss a few here below:

1. Using the innerHTML property
You could retreive the raw HTML from the browser through the WebBrowser.OleObject.Document.documentElement.innerHTML property and then save it to disk.
If you want only the text from the document, and not the HTML, you could use the WebBrowser.OleObject.Document.documentElement.innerText method.
Saving to disk could be easily done using a TStringList object.

2. Through the IPersistFile interface
Although the above method works, there is a much more elegant way to achieve this.
IHTMLDocument implements an interface called IPersistFile which is an common interface for loading and saving objects.
Save the current loaded webpage using the IPersistFiles as follows:

(WebBrowser.Document as IPersistFile).Save('c:\inetpub\wwwroot\MyWebsite\index.html', True);

Besides Save, IPersistFile offers a method for loading files (Load) and a method to find out if the current loaded page is changed compared to when it was last saved. (IsDirty)

For more information on IPersistFile visit the msdn website here.
According this page IPersistFile is new in the .NET Framework 2.0.


william said...

It is so good to see something very interesting in your site that I can't stop on reading.
This is so good article ever. Thank you for sharing and for inspiring your readers. Have a good day!

yosabrams0918 said...

Good day! I simply want to give a huge thumbs up for the good data you might have right here on this post. I can be coming back to your blog for more soon. casino slots

Use an image as your UIBarButtonItem

Using an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem...