-1

We have a DelphiXE program that creates HTML documents using TWebBrowser and stores only their bodies in sql database.

Now we want to add images into the documents. When we add an image then in src attribute we have

src="file:///D:/Projects/Test/IMAGES/img_1.GIF"

D:/Projects/Test is the location of the program and of the HTML file.

We want to have relative paths in src attribute, so when we change the src attribute

s:=elem.outerHTML; 
s:='<IMG border=0 hspace=0 alt="pic 2" src="./IMAGES/955_2.GIF">';
// or s:='<IMG border=0 hspace=0 alt="pic 2" src="IMAGES/955_2.GIF">';
elem.outerHTML:=S

then src attribute does not change, it remains the same

src="file:///D:/Projects/Test/images/Img_1.GIF"

We set the base dir in the HTML file with Notepad to

<BASE href="D:\Projects\Test\">

with no success.

How we can change src attribute to have there relative paths?

Avrob
  • 533
  • 2
  • 10
  • 20
  • 2
    "But we make any change ": How **exactly** are you making the changes you are asking about? This q needs an MCVE (http://stackoverflow.com/help/mcve) Readers should not have to guess this kind of detail. – MartynA Feb 11 '17 at 18:32
  • 1
    In that case, your code should be in the q, not in a comment. Please edit your q. – MartynA Feb 11 '17 at 19:11

1 Answers1

1

Assuming the following HTML:

<body>
    <img border=0 alt="pic 2" src="file:///D:/Projects/Test/IMAGES/img_1.GIF">
    <p>Hello World</p>
</body>

And assuming this has been loaded into a TWebBrowser component instance called WebBrowser1,

use SetAttribute instead of outerHTML as follows:

var
  Body    : IHTMLElement;
  Children: IHTMLElementCollection;
  Element : IHTMLElement;

begin
  Body := (WebBrowser1.Document as IHTMLDocument2).body;
  Children := Body.children as IHTMLElementCollection;
  Element := Children.item( 0, 0 ) as IHTMLElement;
  Element.setAttribute( 'src', './img_1.GIF', 0 );

  // To prove a point:
  ShowMessage( Element.outerHTML );
end;