Solved

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

Posted on 2004-09-23
10
1,204 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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now