Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Internet (https://www.clubdelphi.com/foros/forumdisplay.php?f=3)
-   -   Obtener un fichero adjunto a un nodo de un fichero XML (https://www.clubdelphi.com/foros/showthread.php?t=33822)

muntasil 18-07-2006 12:57:57

Obtener un fichero adjunto a un nodo de un fichero XML
 
Hola foreros, necesito "recoger" un fichero codificado en Base64 que se encuentra "pegado" dentro de un nodo de un fichero XML codificado en UTF-8.
Ahora mismo tengo el problema de que obtengo el fichero pero parece estar corrupto, posiblemente porque se me pase por alto el convertir la cadena de texto del nodo en cuestión en algun formato antes de guardar como archivo.

Os pongo aki el codigo que actualmente estoy usando a ver si así os es mas facil responderme, gracias de antemano.

///////////////////////////////////////////////////////////////////////////////
const
Base64Code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+ 'abcdefghijklmnopqrstuvwxyz'
+ '0123456789+/';
Pad = '=';

type
EBase64Error = Exception;

/////////////////////////////////////////////////////////////////////////////
Procedure TDocumento.StrToFile(Texto: WideString; psNombre: String);
var
Stream: TStream;
Stream2: TBlobStream;
Texto2:String;

begin
Texto2:=Base64ToStr(UTF8Decode(texto));
Stream := TFileStream.Create(psNombre, fmCreate);
try
Stream.WriteBuffer(Pointer(Texto2)^, Length(Texto2));
finally
Stream.Free;
end;
End;

////////////////////////////////////////////////////////////////
function TDocumento.Base64ToStr( B64: string ): string;
var
i,PadCount : integer;
Block : string[3];
x1, x2, x3 : byte;
begin

[i] // input _must_ be at least 4 chars long,
// or multiple of 4 chars
if ( Length( B64 ) < 4 )
or ( Length( B64 ) mod 4 <> 0 ) then
raise EBase64Error.Create( 'Base64ToStr: illegal input length!' );
//
PadCount := 0;
i := Length( B64 );
// count padding chars, if any
while (B64[i] = Pad)
and (i > 0 ) do
begin
inc( PadCount );
dec( i );
end;
//
Result := '';
i := 1;
SetLength( Block, 3 );
while i <= Length( B64 ) - 3 do
begin
// reverse process of above
x1 := ( Char2Idx( B64 ) shl 2 ) or ( Char2IDx( B64[i + 1] ) shr 4 );
Result := Result + Chr( x1 );
x2 := ( Char2Idx( B64[i + 1] ) shl 4 ) or ( Char2IDx( B64[i + 2] ) shr 2 );
Result := Result + Chr( x2 );
x3 := ( Char2Idx( B64[i + 2] ) shl 6 ) or ( Char2IDx( B64[i + 3] ) );
Result := Result + Chr( x3 );
inc( i, 4 );
end;

// delete padding, if any
while PadCount > 0 do
begin
Delete( Result, Length( Result ), 1 );
dec( PadCount );
end;
end;

/////////////////////////////////////////////////////////////////////

[i]function TDocumento.Char2IDx( c: char ): byte;
var
i : integer;
begin
for i := 1 to Length( Base64Code ) do
if Base64Code = c then
begin
Result := pred( i );
EXIT;
end;
Result := Ord( Pad );
end;


La franja horaria es GMT +2. Ahora son las 09:11:06.

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