Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2014-02

View Graphic File

3 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Allan E. Registos

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//
/
Thanks,,
Allan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140220/36a86123/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4267 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140220/36a86123/attachment-0001.bin>

Reply by email

Antonio Fortuny
to Allan E. Registos

Le 20/02/2014 07:43, Allan E. Registos a ?crit :

Show quoted text 37 lines
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,,

Show quoted text 8 lines
Allan -- _______________________________________________ Lazarus mailing list 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

Reply by email

Allan E. Registos
to Antonio Fortuny
Antonio Fortuny wrote 88 lines
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.

Many Thanks! Antonio. I will use and save your code for future use.

Regards,
Allan

Show quoted text 27 lines
Antonio. Thanks,, > Allan > > > -- > _______________________________________________ > Lazarus mailing list > > 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 -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4267 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20140221/197bd2c7/attachment.bin>

Reply by email