February 22, 2012

How to download files from database using Free Pascal/Lazarus

Posted in Databases, files at 8:13 am by أبو إياس

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;

3 Comments »

  1. [...] you can download it using this example Share this:TwitterFacebookLike this:LikeBe the first to like this [...]

  2. alcatel said,

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

  3. May be it can be done by using threads. If I had a time I could try it and let you know.


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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.