[Lazarus] View Graphic File
Antonio Fortuny
a.fortuny at sitasoftware.lu
Thu Feb 20 10:07:01 CET 2014
Le 20/02/2014 07:43, Allan E. Registos a écrit :
> Hi all,
>
> A noob question, I have trouble trying to port an existing vb.net
> snippet to pascal.
> Can anyone provide a code snippet where we can download and view a
> file from a Database? A binary(jpeg/pdf file) was uploaded to a
> database table. The code will just download the file uploaded and view
> it.
>
> In vb.net this can be done:
>
> / if conn.State = ConnectionState.Closed Then conn.Open()//
> // sql = "Select binfile from graphicfiles WHERE id=" & id//
> //
> // cmd = New NpgsqlCommand(sql, conn)//
> //
> // Dim fileData As Byte() = DirectCast(cmd.ExecuteScalar(),
> Byte())//
> //
> // Dim sTempFileName As String = Path.GetTempPath & "\" &
> sFileName//
> //
> // If Not fileData Is Nothing Then//
> //
> // 'Read image data into a file stream //
> // Using fs As New FileStream(sTempFileName,
> FileMode.OpenOrCreate, FileAccess.Write)//
> // fs.Write(fileData, 0, fileData.Length)//
> // 'Set image variable value using memory stream. //
> // fs.Flush()//
> // fs.Close()//
> // End Using//
> //
> // Process.Start(sTempFileName) 'Open file//
> //
> // End If//
> /
Sounds quite easy to translate:
1. install the sqldblaz package if not yet done
2. onto a form or a datamodule drop a connection named DB (correcpondig
to your DB) and a transaction named TR; link them
3. drop a TSqlQuery, link it to the connection and the transacton just
dropped (name Q)
4. fill in the Sql property/. /I assume that the column binfile is a
binay blob
...
var
fs: TMemoryStream;
begin
...
fs := TMemoryStream.Create;
try
DB.DatabaseName := 'whatever connection could be';
DB..UserName := 'sysdba';
DB.Password := 'your password';
DB.Connected := True;
Q.Sql.Text := Format('Select binfile from graphicfiles WHERE
id=%d/'/, [id]);/ // assuming id is an integer, adapt the format
otherwise/
TR.StartTransaction;
Q.Prepare;
Q.Open;
if Q.Eof = False then begin
fs.Position := 0;
TBlobField(Q.FieldByName('binfile')).SaveToStream(fs);
...
fs.Position := 0; // reset position to beginning of stream
do whatever you need to do with the stream "fs"
use stream length by fs.Size
fs.SaveToFile('any file name');
// for an image dropped onto a form:
Image.Picture.LoadFromStream(fs);
... etc...
...
end;
Q.Close;
finally
TR.Commit
fs.Free
end
This should work as it is a code snippet used in one of my programs
(cleaned and simplified). In my case the DBMS is Firebird but this
should work with any other supported by SqlDbLaz package.
Antonio.
Thanks,,
> Allan
>
>
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus
---
Ce courrier électronique ne contient aucun virus ou logiciel malveillant parce que la protection avast! Antivirus est active.
http://www.avast.com
More information about the Lazarus
mailing list