February 22, 2012
How to upload files into database using Free Pascal/Lazarus
To upload files into database, such as photos or any other files type, you need first to create a Blob field in a proper table structure in your favourite database engine. For this example we are using FireBird database table:
create table PHOTOS ( PHOTOID INTEGER, FILENAME VARCHAR(50) , CONTENT BLOB , primary key (PHOTOID) );
Create a new Lazarus application and add the following components:
TButton, TIBConnection, TSQLTransaction, TSQLQuery, and TOpenDialog:
Then add db unit to uses Clause.
on button’s OnClick write this code:
procedure TForm1.Button1Click(Sender: TObject);
begin
if OpenDialog1.Execute then
begin
SQLQuery1.SQL.Text:= 'insert into Photos (FileName, Content) values (:FileName, :Content)';
SQLQuery1.ParamByName('FileName').AsString:= ExtractFileName(OpenDialog1.FileName);
SQLQuery1.ParamByName('Content').LoadFromFile(OpenDialog1.FileName, ftBlob);
SQLQuery1.ExecSQL;
SQLTransaction1.Commit;
end;
end;
AFter running this sample you can load the entire file contents into your database.
Later you can download it using this example

How to download files from database using Free Pascal/Lazarus « Free Pascal Answers said,
February 22, 2012 at 8:13 am
[...] uploading files in the previous example into database, now we need to download that [...]
alcatel said,
March 15, 2012 at 1:00 pm
Is it possible to display a progres indicator which could be important especially for big files?
Abdelkader said,
April 18, 2012 at 10:09 am
salem alakom
can you upload the project , because i have same problem with ftBlob
أبو إياس said,
April 18, 2012 at 12:13 pm
Wa Alikum alsalam, what did you mean by the same problem?
Abdelkader said,
April 18, 2012 at 1:04 pm
السلام عليكم
شكرا على الاجابة يا أخي ابو اياس
لقد كان ينقصني use db unit
لكني الآن عندي مشكل آخر وهو كيفية استعمال TDBImage
جزاك الله عنا كل خير
أبو إياس said,
April 18, 2012 at 1:56 pm
I’m using normal TImage componet, look at this code:
var BlobStream : TStream; begin Screen.Cursor:= crSQLWait; BlobStream := CreateBlobStream(SQLQuery1.FieldByName('Contents'),bmRead); BlobStream.Position:= 0; Image1.Picture.LoadFromStream(BlobStream); Screen.Cursor:= crDefault; BlobStream.Free; endAbdelkader said,
April 18, 2012 at 2:59 pm
الله يفتح عليك و ينورك
Abdelkader said,
April 22, 2012 at 10:23 am
السلام عليكم و رحمة الله و بركاته
كيف الحال يا أخي أبو إياس
ممكن لو سمحت مثال عن طريق
dbimage
و جزاك الله خيرا و زادك من علمه
أبو إياس said,
April 22, 2012 at 10:54 am
I couldn’t make dbImage work, it generates “Stream read error” message
Abdelkader said,
April 22, 2012 at 11:30 am
الله ينور عليك على سرعة الإجابة
سأحاول انشاء الله العمل لحل المشكل و ان امكنني ذلك
سأرسل لك شرحا لتعم الفائدة
بارك الله فيك يا أخي أبو إياس
أخوك عبد القادر من الجزائر
Abdelkader said,
April 22, 2012 at 1:37 pm
السلام عليكم يا أخي ابو إياس
الحمد لله تمكنت من استعمال
dbimage
وهو الآن يعمل كأنه
dbEdit
بكل سهولة و مع فورمات متعدد
لحد الان bmp و jpeg
وأيضا مع سجلات ثنائية الاتجاه
سأبعث إليك انشاء الله الحل مع المثال لتعم الفائدة
أبو إياس said,
April 23, 2012 at 5:10 pm
Can you please explain what did you do for “Stream read error”
Abdelkader said,
May 8, 2012 at 1:37 pm
السلام عليكم و رحمة الله و بركاته
استسمحك على التأخير نظرا لظروف امر بها ادعوا لي بالتوفيق
اما بخصوص الطريقة التي اتبعتها فهي كالتالي:
اضف زر به الكود الآتي
if OpenDialog1.Execute then
begin
Datasource1.DataSet.Edit;
DBImage1.Picture.LoadFromFile(OpenDialog1.FileName);
end;
Abdelkader said,
May 8, 2012 at 1:39 pm
و استعمل TDBImage
لكن بعد ان تقوم بتحميل الصور في القاعدة عن طريق الطريقة السابقة