0

Howto copy MHT (HTML + pictures) data stored in a TMemoryStream to a TWebBrowser? If I save the MHT from the MemoryStream to disk with:

MS.SaveToFile(FName);

and then navigate the WebBrowser to this file:

WebBrowser1.Navigate(FName);

it works fine.

I want to do it directly, without disk saving.

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Reron
  • 181
  • 11
  • Just a wild thought: a .mht file is in fact plain text. So load a TStringList from the TMemoryStream (using LoadFromStream) and then use TWebBrowser's LoadFromStrings to load the TStringList's Text. IOW: `YourStringList.LoadFromStream(YourMemoryStream, ); YourBrowser.LoadFromStrings(YourStringList.Text, '');`. I don't know if that works for MHT, or only for plain HTML, sorry. But you could give it a try. – Rudy Velthuis Jan 05 '18 at 23:49
  • 1
    You can also load a stream into a `TWebBrowser` by navigating it to `about:blank`, then query its `Document` for `IPersistStreamInit` and pass the stream to its `Load()` method (you can convert a `TStream` to `IStream` by using `TStreamAdapter`) – Remy Lebeau Jan 06 '18 at 00:12
  • @Remy: I guess that is the better path. Loading a MHT from a TStringList.Text just shows the plain text. – Rudy Velthuis Jan 06 '18 at 00:16
  • 1
    @RemyLebeau: Tried it your way. Loads fine with SHDocVw.TWebBrowser (VCL WebBrowser), but not with the FMX WebBrowser while my way works in FMX but not in VCL. But even your way displays the MHT as plain text. So there is probably no viable alternative to saving to a .mht file and then navigating to that file, as the OP already did. Loading it into the browser directly simply does not tell the browser that it should treat it as MHT and not as HTML. – Rudy Velthuis Jan 06 '18 at 06:33
  • 1
    The _really_ _really_ hard way to do it is implementing IInternetProtocol. It's a lot of work, some things are really not straight-forward, but I once did, and I'm glad I pulled through, as eventually it has led me to write [xxm](https://github.com/stijnsanders/xxm#xxm) – Stijn Sanders Jan 06 '18 at 18:20
  • can't think any reason why the FMX browser won't work the same as VCL other than a difference in FEATURE_MIME_SNIFFING settings in registry. If you want your code work with MIME sniffing off, implement IMoniker to provide a url that ends with .mht and load the moniker with IPersistMoniker. – Sheng Jiang 蒋晟 Jan 12 '18 at 08:30

0 Answers0