Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

Save webbrowser component containing ONLY text as a text file (500 points)

I  have the webbrowser component displaying the result of a cgi script - just text without any images, etc.

I want this to be saved automatically as a text file in a specific directory (ie, saved as aaa.txt in a directory called "files" which I already have in the same directory as the software).

I am using Delphi 6.  This is URGENT so I am awarding 500 points for this.  Thanks.
0
rincewind666
Asked:
rincewind666
  • 2
2 Solutions
 
2266180Commented:
this works for anything from the twebrowser (text or html)

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OleCtrls, SHDocVw;

type
  TForm1 = class(TForm)
    WebBrowser1: TWebBrowser;
    procedure FormCreate(Sender: TObject);
    procedure WebBrowser1DocumentComplete(Sender: TObject;
      const pDisp: IDispatch; var URL: OleVariant);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses activex;

procedure TForm1.FormCreate(Sender: TObject);
begin
  WebBrowser1.Navigate('http://www.ciuly.com');
end;

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
  PersistStreamInit: IPersistStreamInit;
  StreamAdapter: IStream;
  FileStream: TFileStream;
begin
  if WebBrowser1.Document.QueryInterface(IPersistStreamInit,
    PersistStreamInit) = S_OK then
  begin
      FileStream:= TFileStream.Create('c:\aaa.txt',fmCreate);
      try
        StreamAdapter:= TStreamAdapter.Create(FileStream, soOwned);
        PersistStreamInit.Save(StreamAdapter,true);
      except
        FileStream.Free;
        raise;
      end;
  end;
end;

end.
0
 
2266180Commented:
ups. forgot to remove the test page. sorry for that.
0
 
TNameCommented:
Hi, ciuly's solution is perfect, but here's another way to do it (now that I've typed it... ;)

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
var
 aText:String;
 fs:TFileStream;
 p:Pointer;
begin
  aText:=IHTMLDocument2(WebBrowser1.Document).Body.innerText;
  fs:=TFileStream.Create(ExtractFilePath(Paramstr(0))+'files\aaa.txt',fmCreate);
  p:=pointer(aText);
  fs.Write(p^, Length(aText));
  fs.Free;
end;
0
 
rincewind666Author Commented:
Thanks to both of you. Both work - hard to decide.  Therefore I will split the points.
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.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now