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

Advertisement

26 thoughts on “How to upload files into database using Free Pascal/Lazarus

  1. Pingback: How to download files from database using Free Pascal/Lazarus « Free Pascal Answers

  2. Is it possible to display a progres indicator which could be important especially for big files?

  3. السلام عليكم
    شكرا على الاجابة يا أخي ابو اياس
    لقد كان ينقصني use db unit
    لكني الآن عندي مشكل آخر وهو كيفية استعمال TDBImage
    جزاك الله عنا كل خير

  4. 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;
      end
    
    
  5. السلام عليكم و رحمة الله و بركاته
    كيف الحال يا أخي أبو إياس
    ممكن لو سمحت مثال عن طريق
    dbimage
    و جزاك الله خيرا و زادك من علمه

  6. الله ينور عليك على سرعة الإجابة
    سأحاول انشاء الله العمل لحل المشكل و ان امكنني ذلك
    سأرسل لك شرحا لتعم الفائدة
    بارك الله فيك يا أخي أبو إياس
    أخوك عبد القادر من الجزائر

  7. السلام عليكم يا أخي ابو إياس
    الحمد لله تمكنت من استعمال
    dbimage
    وهو الآن يعمل كأنه
    dbEdit
    بكل سهولة و مع فورمات متعدد
    لحد الان bmp و jpeg
    وأيضا مع سجلات ثنائية الاتجاه
    سأبعث إليك انشاء الله الحل مع المثال لتعم الفائدة

  8. السلام عليكم و رحمة الله و بركاته
    استسمحك على التأخير نظرا لظروف امر بها ادعوا لي بالتوفيق
    اما بخصوص الطريقة التي اتبعتها فهي كالتالي:
    اضف زر به الكود الآتي
    if OpenDialog1.Execute then
    begin
    Datasource1.DataSet.Edit;
    DBImage1.Picture.LoadFromFile(OpenDialog1.FileName);

    end;

  9. و استعمل TDBImage
    لكن بعد ان تقوم بتحميل الصور في القاعدة عن طريق الطريقة السابقة

  10. Pingback: Firebird News » How to upload/download files from database using Free Pascal/Lazarus

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s