[Example] How to store HTML in database

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 13470
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[Example] How to store HTML in database

Post by Sergey Tkachenko » Tue May 12, 2009 4:29 pm

TDBRichViewEdit can store data in the following formats: RVF, RTF, text, depending on the value of FieldFormat property.
If you store data in another format, such as HTML, you need to process OnSaveCustomFormat and OnLoadCustomFormat events.

Example 1: using TrvHtmlImporter

Code: Select all

procedure TForm1.DBRichViewEdit1SaveCustomFormat(Sender: TCustomRichView;
  Stream: TStream; var DoDefault: Boolean);
begin

  Sender.SaveHTMLToStreamEx(Stream, ExtractFilePath(Application.ExeName),
    'title', 'img', '', '', '', []);
  {
  or
  Sender.SaveHTMLToStream(Stream, ExtractFilePath(Application.ExeName),
    'title', 'img', []);
  }
  DoDefault := False;
end;

procedure TForm1.DBRichViewEdit1LoadCustomFormat(Sender: TCustomRichView;
  Stream: TStream; var DoDefault: Boolean);
var s: String;
begin
  SetLength(s, Stream.Size);
  Stream.ReadBuffer(PChar(s)^, Length(s));
  RvHtmlImporter1.LoadHtml(s);
  DoDefault := False;
end;
If your documents have images, this example stores them in the application directory (with autogenerated names, so there will be many unused images). You can process OnImportPicture and OnSaveImage2 events to store them in database instead.

Example 2: using TRvHtmlViewImporter:

All the same, except for

Code: Select all

procedure TForm1.DBRichViewEdit1LoadCustomFormat(Sender: TCustomRichView;
  Stream: TStream; var DoDefault: Boolean);
var s: String;
begin
  HTMLViewer1.LoadFromStream(Stream);
  RVHTMLViewImporter1.ImportHtmlViewer(HTMLViewer1, Sender as TCustomRichViewEdit);
  DoDefault := False;
end;

Post Reply