código rodando legal
Gerar o arquivo
1º é criado o arquivo xml seguindo o leiaute 2.0
2º é gravado no banco de dados em uma tabela o nome do arquivo xml ex: c:\sistema\sngpc\mov_0105_a_0305.xml, o hash, e status de controle, e um campo de mensagem (blob)
Estrutura do arquivo de controle
id_arquivo auto
Data date
Arquivo varchar(60) // Ex 'C:\Sistema\sngpc\mov_0105_a_0605.xml'
Mensagem blob
Hash varchar(32)
status varchar(1) valores 1,2 2=Arquivo transmitido
procedure TFArquivos.btTransmitirClick(Sender: TObject);
var strArquivo:String;
bdaArquivo:TByteDynArray;
xmlArquivo:TXMLDocument;
strHash:String;
strMens:String;
wsEmail,wsSenha:WideString;
begin
// dmFar.IBDSsngpc é uma tabela onde contém os dados nome do arquivo,hash,mensagem de retorno e status de trasmissão para controle do usuário
if dmFar.IBDSsngpc['status']='2' then showMessage('Arquivo já foi transmitido.');
bdaArquivo:=Nil;
if dmFar.IBDSsngpc['status']='1' then
begin
screen.Cursor:=crHourGlass;
strArquivo:=dmFar.IBDSsngpc['NomeArquivo'];
if not fileexists(strArquivo) then
begin
screen.Cursor:=crDefault;
showMessage('Não encontrei o arquivo '+strArquivo);
exit;
end;
wsEmail:=FPrincipal.stremail;
wsSenha:=FPrincipal.strsenha;
xmlArquivo:=TXMLDocument.Create(Self);
xmlArquivo.LoadFromFile(strArquivo);
strHash :=dmFar.IBDSsngpc['hash']; // gravado na tabela
strArquivo:=ExtractFileName(strArquivo);
bdaArquivo:=FileToByteArray(zipar(strArquivo));
FMensagemSNGPC:=TFMensagemSNGPC.Create(Self);
strmens:=GetsngpcSoap(blnHom,False,'',nil).ValidarUsuario(wsEmail,wsSenha);
FMensagemSNGPC.memo1.Lines.Add('Arquivo: '+strArquivo);
FMensagemSNGPC.memo1.Lines.Add('Mensagem de usuário e senha: '+strMens);
FMensagemSNGPC.memo1.Lines.Add(strArquivo);
strmens:=GetsngpcSoap(blnHom,False,'',nil).EnviaArquivoSNGPC(wsEmail,wsSenha,bdaArquivo,strHash);
FMensagemSNGPC.memo1.Lines.Add('');
FMensagemSNGPC.memo1.Lines.Add('');
FMensagemSNGPC.memo1.Lines.Add('=============================================================');
FMensagemSNGPC.memo1.Lines.Add('Mensagem de envio: '+strMens);
FMensagemSNGPC.memo1.Lines.Add('=============================================================');
FMensagemSNGPC.ShowModal;
dmFar.IBDSsngpc.Edit;
dmFar.IBDSsngpc['status']:='2';
dmFar.IBDSsngpc['mensagem']:=FMensagemSNGPC.memo1.Text;
dmFar.IBDSsngpc.Post;
FMensagemSNGPC.Free;
xmlArquivo.Free;
screen.Cursor:=crDefault;
end;
end;
// funçoes auxiliares
function TFArquivos.Zipar(strArquivo:string):String; // função para compactar e retornar o nome do arquivo
var tsqZip1:TtsqZip;
begin
tsqZip1:=TtsqZip.Create(Self);
tsqZip1.FilesToZip.Add(strArquivo);
tsqZip1.ZipFileName:=copy(strArquivo,1,length(strArquivo)-3)+'zip';
tsqZip1.Execute;
result:=tsqZip1.ZipFileName;
end;
function TFArquivos.FileToByteArray(const FileName: String): TByteDynArray;
const BLOCK_SIZE=1024;
var BytesRead, BytesToWrite, Count : Integer;
F : File of Byte;
pTemp : Pointer;
begin
AssignFile( F, FileName );
Reset(F);
try
Count := FileSize(F);
SetLength(Result, Count);
pTemp := @Result[0];
BytesRead := BLOCK_SIZE;
while (BytesRead = BLOCK_SIZE) do
begin
BytesToWrite := Min(Count, BLOCK_SIZE);
BlockRead(F, pTemp^, BytesToWrite , BytesRead );
pTemp := Pointer(LongInt(pTemp) + BLOCK_SIZE);
Count := Count-BytesRead;
end;
finally
CloseFile( F );
end;
end;