Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to save and retrieve a file to/ from a blob field Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
28-Aug-02
Category
Files Operation
Language
Delphi 2.x
Views
115
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

How can I save a file in an Access database. I would like to save zip files in an 
Access mdb database. (OLE OBJECT Field) BLOB FIELD

Answer:

Solve 1:

Here's a unit that demonstrates getting a file to/ from a blob field:

1   unit FileToFromDB;
2   
3   interface
4   
5   uses
6     Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7     StdCtrls, FileCtrl, DBCtrls, ExtCtrls, Db, DBTables, Menus;
8   
9   type
10    TFrmFileToFromDB = class(TForm)
11      CbxDriveSelect: TDriveComboBox;
12      LbxDirSelect: TDirectoryListBox;
13      LbxFileSelect: TFileListBox;
14      BtnToDB: TButton;
15      DataSource1: TDataSource;
16      Table1: TTable;
17      Table1TheLongInt: TIntegerField;
18      Table1ABlobField: TBlobField;
19      Table1Bytes1: TBlobField;
20      Table1Bytes2: TBytesField;
21      Table1B32_1: TBlobField;
22      Table1B32_2: TBytesField;
23      DBNavigator1: TDBNavigator;
24      BtnFromDB: TButton;
25      Memo1: TMemo;
26      LblCopyright: TLabel;
27      procedure FormCreate(Sender: TObject);
28      procedure FormDestroy(Sender: TObject);
29      procedure BtnToDBClick(Sender: TObject);
30      procedure BtnFromDBClick(Sender: TObject);
31      procedure LbxFileSelectChange(Sender: TObject);
32      procedure DataSource1DataChange(Sender: TObject; Field: TField);
33    private
34      { Private declarations }
35    public
36      { Public declarations }
37    end;
38  
39  var
40    FrmFileToFromDB: TFrmFileToFromDB;
41  
42  implementation
43  
44  {$R *.DFM}
45  
46  procedure TFrmFileToFromDB.FormCreate(Sender: TObject);
47  begin
48    Table1.Open;
49    Memo1.Clear;
50  end;
51  
52  procedure TFrmFileToFromDB.FormDestroy(Sender: TObject);
53  begin
54    Table1.Close;
55  end;
56  
57  procedure TFrmFileToFromDB.BtnToDBClick(Sender: TObject);
58  var
59    curNdx: Integer;
60    f: Integer;
61    theBlobStream: TBlobStream;
62    thisFile: TFileStream;
63  begin
64    curNdx := LbxFileSelect.ItemIndex;
65    LbxFileSelect.Items.BeginUpdate;
66    try
67      for f := 0 to LbxFileSelect.Items.Count - 1 do
68      begin
69        if LbxFileSelect.Selected[f] then
70        begin
71          LbxFileSelect.ItemIndex := f;
72          Table1.Edit;
73          theBlobStream := TBlobStream.Create(Table1B32_1, bmReadWrite);
74          try
75            thisFile := TFileStream.Create(LbxFileSelect.Filename, fmOpenRead);
76            try
77              theBlobStream.CopyFrom(thisFile, thisFile.Size);
78            finally
79              thisFile.Free;
80            end;
81          finally
82            theBlobStream.Free;
83          end;
84          Table1.Post;
85          Table1.Next;
86        end;
87      end;
88      Table1.First;
89    finally
90      LbxFileSelect.ItemIndex := curNdx;
91      LbxFileSelect.Items.EndUpdate;
92    end;
93  end;
94  
95  procedure TFrmFileToFromDB.BtnFromDBClick(Sender: TObject);
96  var
97    hdr: array[1..3] of byte;
98    thisFile: TMemoryStream;
99    thisText: TMemoryStream;
100   theBlobStream: TBlobStream;
101 begin
102   thisFile := TMemoryStream.Create;
103   Memo1.Clear;
104   try
105     theBlobStream := TBlobStream.Create(TBlobField(Table1.FieldByName('B32_1')), 
106 bmRead);
107     try
108       thisFile.CopyFrom(theBlobStream, theBlobStream.Size);
109       thisFile.Seek(0, soFromBeginning);
110     finally
111       theBlobStream.Free;
112     end;
113     thisFile.read(hdr[1], 3);
114     thisFile.Seek(0, soFromBeginning);
115     if (hdr[1] = $FF) and (hdr[2] = $0A) and (hdr[3] = $0) then
116     begin
117       thisText := TMemoryStream.Create;
118       try
119         try
120           ObjectResourceToText(thisFile, thisText); {DFM to text.}
121           thisText.Seek(0, soFromBeginning); {Text stream to beginning}
122           Memo1.Lines.LoadFromStream(thisText); {Text to memo1}
123         except
124           Memo1.Lines.Add('Invalid DFM image');
125         end;
126       finally
127         thisText.Free;
128       end
129     end
130     else
131     begin
132       Memo1.Lines.Add('Invalid DFM image');
133     end;
134   finally
135     thisFile.Free;
136   end;
137 end;
138 
139 procedure TFrmFileToFromDB.LbxFileSelectChange(Sender: TObject);
140 begin
141   BtnToDB.Enabled := LbxFileSelect.SelCount > 0;
142 end;
143 
144 procedure TFrmFileToFromDB.DataSource1DataChange(Sender: TObject; Field: TField);
145 begin
146   BtnFromDB.Click;
147 end;
148 
149 end.



Solve 2:

If you develop a database related software, then very popular task is to save some 
files (documents/images/reports/etc) in some BLOB field of table and use these 
saved data later. 

In this tip I want to show how this task could be solved. 

To save a file to BLOB: 
150 
151 blob := yourDataset.CreateBlobStream(yourDataset.FieldByName('YOUR_BLOB'),
152   bmWrite);
153 try
154   blob.Seek(0, soFromBeginning);
155 
156   fs := TFileStream.Create('c:\your_name.doc', fmOpenRead or
157     fmShareDenyWrite);
158   try
159     blob.CopyFrom(fs, fs.Size)
160   finally
161     fs.Free
162   end;
163 finally
164   blob.Free
165 end;
166 
167 //to load from BLOB:
168 
169 blob := yourDataset.CreateBlobStream(yourDataset.FieldByName('YOUR_BLOB'),
170   bmRead);
171 try
172   blob.Seek(0, soFromBeginning);
173 
174   with TFileStream.Create('c:\your_name.doc', fmCreate) do
175   try
176     CopyFrom(blob, blob.Size)
177   finally
178     Free
179   end;
180 finally
181   blob.Free
182 end;


Using this code you can work with any database engine (BDE/ADO/DAO/ODBC/etc) and any file format (document of MS Word, spreadsheet of MS Excel, bitmap or jpeg pictures, wav-files etc) 

			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC