Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Load html file from TStrings to webbrowser and do not save to harddisk,Thank you.

Posted on 2004-09-23
10
Medium Priority
?
1,314 Views
Last Modified: 2011-10-03
Hello Experts.
I load html file from database(TStrings) to webbrowser do not save to harddisk.

 Thanks you

//load
function ShowHtmls(mWebBrowser: TWebBrowser; mStrings: TStrings): Boolean; overload;
var
  vMemoryStream: TMemoryStream;
begin
  Result := False;
  if not (Assigned(mStrings) and Assigned(mWebBrowser)) then Exit;
  if not Assigned(mWebBrowser.Document) then Exit;
  vMemoryStream := TMemoryStream.Create;
  try
    mStrings.SaveToStream(vMemoryStream);
    try
      vMemoryStream.Position := 0;
      Application.ProcessMessages;
      (mWebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(vMemoryStream));
    except
      Exit;
    end;
  finally
    vMemoryStream.Free;
  end;
  Result := True;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  S: TStringStream;
begin
  S:= TStringStream.Create('<html><h1>Stream Test</h1><p>:This HTML content ' +
         'is being loaded from a stream.</html>');
  try
    LoadStream(WebBrowser1, S);
  finally
    S.Free;
  end;

End;


//save
procedure TForm1.Button2Click(Sender: TObject);
begin
  //WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_PROMPTUSER);
  WebBrowser1.ExecWB(OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT,
                   EmptyParam,
                   EmptyParam);
end;

Result is NUll
0
Comment
Question by:Dlwxn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 12140110
procedure AssignDocument(Browser: TWebBrowser; Text: String);
var
  Document: OleVariant;
  {$ifdef PERSIST_STREAM}
  InStream: TStream;
  Persist: IPersistStreamInit;  {Declared in ActiveX}
  {$endif}
begin
  {$ifdef WRITE_FILE}
  Document := LocalServerPath('temp.html');
  WriteTextFile(Document, Text);  {utility function}
  Browser.Navigate2(Document);
  {$endif}
  {$ifdef PERSIST_STREAM}
  Document := 'about:blank';
  Browser.Navigate2(Document);
  InStream := TStringStream.Create(Text);
  try
    Persist := (Browser.Document as IPersistStreamInit);
    Persist.Load(TStreamAdapter.Create(InStream));
  finally
    InStream.Free;
  end;
  {$endif}
  {$ifdef DISPATCH_DOC}
  Document := 'about:blank';
  Browser.Navigate2(Document);
  Document := Browser.Document as IDispatch;
  Document.Open;
  try
    Document.Write(Text);
  finally
    Document.Close;
  end;
  {$endif}
end;
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12140133
OR:

uses ActiveX;

procedure LoadStream(WebBrowser: TWebBrowser; Stream: TStream);
var
   PersistStreamInit: IPersistStreamInit;
   StreamAdapter: IStream;
   MemoryStream: TMemoryStream;
begin
   WebBrowser.Navigate('about:blank');
   repeat
     Application.ProcessMessages;
     Sleep(0);
   until
      WebBrowser.ReadyState = READYSTATE_COMPLETE;
   if WebBrowser.Document.QueryInterface(IPersistStreamInit, PersistStreamInit) = S_OK then begin
     if PersistStreamInit.InitNew = S_OK then begin
       MemoryStream:= TMemoryStream.Create;
       try
         MemoryStream.CopyFrom(Stream, 0);        
         MemoryStream.Position:= 0;
       except
         MemoryStream.Free;
         raise;
       end;
         StreamAdapter:= TStreamAdapter.Create(MemoryStream, soOwned);
         PersistStreamInit.Load(StreamAdapter);
     end;
  end;
end;

Call with:
..............................
var
  S: TStringStream;
begin
  S:= TStringStream.Create(YourHTMLString');
  try
    LoadStream(WebBrowser1, S);
  finally
    S.Free;
  end;
end;
0
 
LVL 9

Accepted Solution

by:
ginsonic earned 2000 total points
ID: 12140147
Don't understand from your question if you wish to save the HTML doc ... but if the answer is yes ...

function SaveHTMLCode(WebBrowser: TWebBrowser; const FileName: TFileName): Boolean;
var
  ps: IPersistStreamInit;
  fs: TFileStream;
  sa: IStream;
begin
  ps := WebBrowser.Document as IPersistStreamInit;
  fs := TFileStream.Create(FileName, fmCreate);
  try
    sa := TStreamAdapter.Create(fs, soReference) as IStream;
    Result := Succeeded(ps.Save(sa, True));
  finally
    fs.Free;
  end;
end;

Call with:
  SaveHTMLCode(WebBrowser1, 'C:\MyHTMLDoc.html');
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Dlwxn
ID: 12140157
Thank you

How save to harddisk?

Here is my code:

//save
procedure TForm1.Button2Click(Sender: TObject);
begin
  //WebBrowser1.ExecWB(OLECMDID_SAVEAS,OLECMDEXECOPT_PROMPTUSER);
  WebBrowser1.ExecWB(OLECMDID_SAVEAS, OLECMDEXECOPT_DODEFAULT,
                   EmptyParam,
                   EmptyParam);
end;

0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12140173
For save read my previous somment. This save your document to 'C:\MyHTMLDoc.html'
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12140195
Another way:

HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
PersistFile := HTMLDocument as IPersistFile;
PersistFile.Save('t1.tmp', true);

or:
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12140198
or.. not sure if you wish to save not to harddisk but to your database then save the doc to a stream...

uses
  ActiveX;

procedure SaveDocumentSourceToStream(Document: IDispatch; Stream: TStream);
var
  PersistStreamInit: IPersistStreamInit;
  StreamAdapter: IStream;
begin
  Stream.Size := 0;
  Stream.Position := 0;
  if Document.QueryInterface(IPersistStreamInit, PersistStreamInit) = S_OK then
  begin
    StreamAdapter := TStreamAdapter.Create(Stream, soReference);
    PersistStreamInit.Save(StreamAdapter, False);
    StreamAdapter := nil;
  end;
end;
0
 

Author Comment

by:Dlwxn
ID: 12140211
ok ,thank you.

I am Chinese.  
Date: 09/29/2004 13:00PM  Beijing
0
 

Author Comment

by:Dlwxn
ID: 12140243
sorry!

Split Points:

"At least 2 Answers are required"
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12143128
Glad to help you :)

I'm from Romania!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question