Google Groupes n'accepte plus les nouveaux posts ni abonnements Usenet. Les contenus de l'historique resteront visibles.

How to handle relative path references in WebBrowser control?

285 vues
Accéder directement au premier message non lu

Steve Trefethen

non lue,
10 mars 2003, 02:43:3010/03/2003
à
I'm hosting the WebBrowser control and I'd like to know how people handle
relative path references to things like images and links? I want to avoid
modifying the document in order to cause them to load correctly. I've tried
the obvious (at least to me) which was to call SetCurrentDirectory prior to
calling Navigate however that had no effect.

Any thoughts?

Thanks
--
-Steve


Igor Tandetnik

non lue,
10 mars 2003, 10:40:5410/03/2003
à
The links are relative to the URL of the page itself, not to the current
directory or anything. How do you load the page into WebBrowser?
--
With best wishes,
Igor Tandetnik

"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken

"Steve Trefethen" <sdt...@nojunk.bar.hotmail.com> wrote in message
news:ONLN9it5...@TK2MSFTNGP09.phx.gbl...

Steve Trefethen

non lue,
10 mars 2003, 11:58:2110/03/2003
à
Hi Igor,
Thanks very much for your reply and for your contributions to this group
in general.

> The links are relative to the URL of the page itself, not to the current
> directory or anything.

That totally makes sense. My SetCurrentDir attempt was admittedly feeble.
<g>

> How do you load the page into WebBrowser?

I use IPersistStreamInit for streams and IPersistFile for loading from a
filename. Occasionally, I'll use Navigate but not very often.

--
-Steve


Igor Tandetnik

non lue,
10 mars 2003, 12:38:5410/03/2003
à
"Steve Trefethen" <sdt...@nojunk.bar.hotmail.com> wrote in message
news:u75OLZy5...@TK2MSFTNGP10.phx.gbl...

> > How do you load the page into WebBrowser?
>
> I use IPersistStreamInit for streams and IPersistFile for loading from
a
> filename. Occasionally, I'll use Navigate but not very often.

When loading from a stream, there is no absolute URL for the HTML page.
What exactly are relative URLs supposed to refer to?

When loading from a file, why not just navigate to an appropriate
file:// URL?

Steve Trefethen

non lue,
10 mars 2003, 13:53:4510/03/2003
à
> When loading from a stream, there is no absolute URL for the HTML page.
> What exactly are relative URLs supposed to refer to?

That's just it what I need/want is to be able to specify what they refer to.
The stream is actually a file on disk but I can't load it using a file://
URL because it's actually coming from an editor buffer and might have been
modified from the on-disk version. I'm working on an HTML editor with both
a text and design mode where the document's on-disk representation will
likely be different from what is being edited.

> When loading from a file, why not just navigate to an appropriate
> file:// URL?

Perhaps there is another way to do what I'm trying to do but imagine an HTML
editor where the text editor actually contains the HTML and then you load
the text editor's representation of the file which may be different from the
on-disk version. I don't want to create a hack where the file has to be
saved first.

--
-Steve


Igor Tandetnik

non lue,
10 mars 2003, 14:19:2410/03/2003
à
"Steve Trefethen" <sdt...@non.junk.hotmail.com> wrote in message
news:#N3GgZz5...@TK2MSFTNGP11.phx.gbl...

> > When loading from a stream, there is no absolute URL for the HTML
page.
> > What exactly are relative URLs supposed to refer to?
>
> That's just it what I need/want is to be able to specify what they
refer to.
> The stream is actually a file on disk but I can't load it using a
file://
> URL because it's actually coming from an editor buffer and might have
been
> modified from the on-disk version. I'm working on an HTML editor with
both
> a text and design mode where the document's on-disk representation
will
> likely be different from what is being edited.

The easiest way to do this is to embed a <base> tag into the generated
HTML. You don't have to save it to disk, or make visible to the user,
just feed it in the stream with the rest of the content.

Another way is to write a custom implementation of IMoniker interface.
You only need a non-trivial implementation of two methods: BindToStorage
should return the IStream with your HTML content, and GetDisplayName
should return the base URL you want to use to resolve relative links.
You then use IPersistMoniker to feed the content into MSHTML using this
custom implementation, instead of IPersistStreamInit. Disclaimer: I have
not done this myself, but I've seen people reporting successful use of
this technique.

Steve Trefethen

non lue,
10 mars 2003, 17:48:0610/03/2003
à
Hi Igor,
Thanks!

> The easiest way to do this is to embed a <base> tag into the generated
> HTML. You don't have to save it to disk, or make visible to the user,
> just feed it in the stream with the rest of the content.

Yes, I'm avoiding this since I really don't want to mess with the user's
HTML.

> Another way is to write a custom implementation of IMoniker interface.
> You only need a non-trivial implementation of two methods: BindToStorage
> should return the IStream with your HTML content, and GetDisplayName
> should return the base URL you want to use to resolve relative links.
> You then use IPersistMoniker to feed the content into MSHTML using this
> custom implementation, instead of IPersistStreamInit. Disclaimer: I have
> not done this myself, but I've seen people reporting successful use of
> this technique.

This sounds very promising and is definately along the lines of what I was
expecting to have to do.

Thanks
--
-Steve


Steve Trefethen

non lue,
14 mars 2003, 04:36:2914/03/2003
à
Hi Igor,

> > Another way is to write a custom implementation of IMoniker interface.
> > You only need a non-trivial implementation of two methods: BindToStorage
> > should return the IStream with your HTML content, and GetDisplayName
> > should return the base URL you want to use to resolve relative links.
> > You then use IPersistMoniker to feed the content into MSHTML using this
> > custom implementation, instead of IPersistStreamInit.

Just thought I'd followup to say that what you suggested worked just fine.
I'm working in Delphi and implemented a class that implements IMoniker and
changed my LoadFromStream method to QI on the WebBrowser Document for
IPersistMoniker then I passed my implementation of IMoniker and as you said
implemented BindToStorage and GetDisplayName. In BindToStorage I simply use
a TStreamAdapter (IStream for a VCL TStream for those who know Delphi VCL)
and return the stream. Then viola, my images load just fine.

I've only tested it with a single <IMG SRC="foo.jpg"> tag so far but it now
displays the image versus a broken image link. So, thanks once again for
you help, it was much appreciated.

--
-Steve


0 nouveau message