Page 1 of 1

Can TDBRichViewEdit save html in database?

Posted: Mon Jan 23, 2006 7:39 am
by Sam2006
I am writing a program that manage records in my online MySQL - DB about movies, books.

Theres is the field DESCRIPTION in each record that store additional information about each item. Information is stored in html format and I search for it in Internet and want to directly copy to my component. That description is inserted directly to Web page.

When I tried TDBRichView i found that it stores data not in html but as:
"-8 1 3 2 1 0 0 0 0 Options 0 1 0 4 0 0 HTML is OFF 4 1 0 4 0 0 BBCode 0 1 -1 1 0 0 is ON 0 1 0 4 0 0 Smilies are ON"

Is it possible to make TDBRichViewEdit to store info as HTML?

Posted: Mon Jan 23, 2006 6:08 pm
by Sergey Tkachenko
By default, documents are stored in RVF format (internal trichview format). You can change it to RTF or plain text by assigning different value to FieldFormat property.

As for storing in different formats, it's more comlicated, but possible in TRichView since the version 1.9.13

type
TRVCustomFormatEvent = procedure (Sender: TCustomRichView; Stream: TStream; var DoDefault: Boolean) of object;

Events: TDBRichView.OnLoadCustomFormat, TDBRichViewEdit.OnLoadCustomFormat allow loading data from db in your own format. Data should be loaded from Stream to Sender. If it is loaded successfully, set DoDefault to False, otherwise the component will attempt to load RVF/RTF/text.

Event: TDBRichViewEdit.OnSaveCustomFormat allows to save document to db in your own format. If you saved it, set DoDefault to False, otherwise the component will save it according to the FieldFormat property.

Posted: Sun Apr 22, 2007 9:27 am
by Prismax
I still haven't figured out how to load/save HTML to a database field properly using the onloadcustomformat and onsavecustomformat events.

In the onsavecustomformat I just use the savehtmltostream method and it works fine, because when I look into my DB I see plain html code.

But how can I display the rich content again when loading it into the dbrichviewedit component? If it's loaded again I see plain HTML code, like the "source" view of a website, but not the "design" view of that code.

Posted: Wed Apr 25, 2007 6:15 pm
by Prismax
I've been trying lots of other things, but still haven't found the right way to do this. Can anyone help me about what to do?

Posted: Sat Apr 28, 2007 5:42 pm
by Sergey Tkachenko
Use TRVHtmlImporter component (you can download it from http://www.trichview.com/resources/)

Assign RVHtmlImporter.RichView := DBRichViewEdit1.

Code: Select all

procedure TForm1.DBRichViewEdit1SaveCustomFormat(Sender: TCustomRichView;
  Stream: TStream; var DoDefault: Boolean);
begin
  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;
The main problem with this code is images, because they all will be stored in the applications' directory. And every time you post changes in DB, new images will be created.
So, if you documents have images, use OnSaveImage2 or OnHTMLSaveImage to save images in another DB table, and OnImportPicture to read them from there.