Solved

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

Posted on 2004-09-23
10
1,224 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
  • 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 500 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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