Ver Mensaje Individual
  #1  
Antiguo 18-07-2006
muntasil muntasil is offline
Registrado
 
Registrado: jul 2006
Posts: 6
Reputación: 0
muntasil Va por buen camino
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;
Responder Con Cita