Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Conexión con bases de datos (https://www.clubdelphi.com/foros/forumdisplay.php?f=2)
-   -   Campos OID en Postgres y Delphi (https://www.clubdelphi.com/foros/showthread.php?t=35868)

Franz Tejada 25-09-2006 16:09:37

Campos OID en Postgres y Delphi
 
Saludos, necesito hacer una aplicación en la que se pueda insertar imagenes, la única forma que encontre es utilizando campos de tipo OID pero al enviar las imagenes desde el cliente (Delphi en Windows) a mi servidor (Postgres en Linux) no funciona.
Utilizo la función lo_import pero me parece que solo funciona en la maquina local. Les agradecere cualquier sugerencia, código, ejemplo o los que sea :p.

Franz Tejada 03-10-2006 16:29:15

Campos OID en Postgres y Delphi
 
Hola otra vez,

Les comento que lei en el Inter que la mejor forma de guardar una imagen o cualquier archivo en Postgres es como texto pero aun no puedo hacerlo.

Si alguien sabe como codificar una imagen de forma que pueda almacenarla en un campo de texto o binario se le agradeceria bastante si lo publicara. Gracias

seoane 03-10-2006 23:34:14

Yo de bases de datos se lo justo, normalmente no me metería a responder una pregunta sobre este tema, pero con lo de guardar una imagen como si fuera texto, ahí si que te puedo ayudar.

Código Delphi [-]
function BinToStr(Stream: TStream): string;
var
  b: Byte;
  i: integer;
begin
  Result:= '';
  for i:= 1 to  Stream.Size do
  begin
    Stream.Read(b,1);
    Result:= Result + IntToHex(b,2);
  end;
end;

procedure StrToBin(Stream: TStream; var Str: string);
var
  i: integer;
  b: integer;
begin
  i:= 1;
  while i < Length(Str) do
  begin
    if TryStrToInt('$' + Copy(Str,i,2),b) then
    begin
      Stream.Write(b,1);
    end else
      Exit;
    inc(i,2);
  end;
end;


// Pasar un fichero a un string
var
  Str: string;
  Stream: TFileStream;
begin
  Stream:= TFileStream.Create('d:\1.jpg',fmOpenRead);
  with Stream do
  try
    Str:= BinToStr(Stream);
    // El archivo esta en la cadena de texto, ahora
    // puedes hacer lo que quieras con el texto
  finally
    Free;
  end;
end;

// Pasar un string a un fichero
var
  Str: string;
  Stream: TFileStream;
begin
  // Obten el texto y colocalo en Str
  Stream:= TFileStream.Create('d:\2.jpg',fmCreate);
  with Stream do
  try
    StrToBin(Stream,Str);
  finally
    Free;
  end;
end;

El algoritmo anterior no es una maravilla, de hecho duplica el tamaño necesario para guardar el archivo. Es un comienzo, aunque yo te recomiendo que busques información sobre la codificación base64 mucho mas eficiente.

Franz Tejada 04-10-2006 02:25:36

Gracias por responder a mi pregunta, ya probe el código y me ayudara bastante. Te cuento que ya estube buscando información acerca de Base64 y nada, bueno seguire investigando, bye

burgosrodas 21-01-2011 21:44:31

Base 64
 
Cheka este hilo clubdelphi.com/foros/showthread.php?t=48924
tiene la unidad completa y tiene dos ejemplo de como utilizarlo

Código Delphi [-]
unit UBase64;

interface

uses Windows, SysUtils, Classes;

function BinToStr(Binary: PByte; Len: Cardinal): String;
procedure StrToStream(Str: String; Stream: TStream);

implementation

const
  CRYPT_STRING_BASE64 = 1;

function CryptBinaryToString(pbBinary: PByte; cbBinary: DWORD; dwFlags: DWORD;
  pszString: PChar; var pcchString: DWORD): BOOL; stdcall;
  external 'Crypt32.dll' name 'CryptBinaryToStringA';

function CryptStringToBinary(pszString: PChar; cchString: DWORD; dwFlags: DWORD;
  pbBinary: PByte; var pcbBinary: DWORD; pdwSkip: PDWORD;
  pdwFlags: PDWORD): BOOL; stdcall;
  external 'Crypt32.dll' name 'CryptStringToBinaryA';

function BinToStr(Binary: PByte; Len: Cardinal): String;
var
  Count: DWORD;
begin
  Count:= 0;
  if CryptBinaryToString(Binary,Len,CRYPT_STRING_BASE64,nil,Count) then
  begin
    SetLength(Result,Count);
    if not CryptBinaryToString(Binary,Len,CRYPT_STRING_BASE64,PChar(Result),Count) then
      Result:= EmptyStr;
  end;
end;

procedure StrToStream(Str: String; Stream: TStream);
var
  Buffer: PByte;
  Count: DWORD;
begin
  Count:= 0;
  if CryptStringToBinary(PChar(Str),Length(Str),CRYPT_STRING_BASE64,nil,Count,
    nil,nil) then
  begin
    GetMem(Buffer,Count);
    try
      if CryptStringToBinary(PChar(Str),Length(Str),CRYPT_STRING_BASE64,Buffer,
        Count,nil,nil) then
        Stream.WriteBuffer(Buffer^,Count);
    finally
      FreeMem(Buffer);
    end;
  end;
end;

end.


La franja horaria es GMT +2. Ahora son las 04:08:01.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi