How to download files from database using Free Pascal/Lazarus

After uploading files in the previous example into database, now we need to download that files:

Create new Lazarus project to open and browse Photos table in this form:

Download button will prompt you to specify where to download the file.

Write this code in Download button’s OnClick:


procedure TForm1.Button3Click(Sender: TObject);
var
  BlobStream : TStream;
  M: TFileStream;
begin
  SaveDialog1.FileName:= SQLQuery2.FieldByName('FileName').AsString;
  if SaveDialog1.Execute then
  begin
    BlobStream := SQLQuery2.CreateBlobStream(SQLQuery2.FieldByName('Content'),bmread);
    M:= TFileStream.Create(SaveDialog1.FileName, fmCreate);
    BlobStream.Position:= 0;
    M.CopyFrom(BlobStream, BlobStream.Size);
    M.Free;
    BlobStream.Free;
  end;

end;

7 thoughts on “How to download files from database using Free Pascal/Lazarus

  1. Pingback: How to upload files into database using Free Pascal/Lazarus « Free Pascal Answers

  2. we can do it with one line :(if we use DBImage1, see my message in previous example )

      DBImage1.Picture.SaveToFile(SQLQuery1.FieldByName('Content').AsString);
    
  3. Hello, I want to make you a question: What kind of report tool are you using in your software development?
    I know that exist a report tool called Fortes4Lazarus, but i would like to see if there is another good tool.
    Thanks.

  4. I’m using Fortes report, and I’ve used LazReport, but LazReport has an issues while designing reports under linux.
    Make sure to get the latest version of Fortes report

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s