Solved

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

Posted on 2004-09-23
10
1,217 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

786 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