Buenas de nuevo gente. a ver una pregunta.
Tengo una camara IP, que va haciendo disparos y guarda la foto como imagen.bmp.
A traves de ftp me conecto a la camara y hago un getfile de imagen.bmp para ir viendo lo que la camara va grabando.
Si hago un FTP a traves de DOS y un get todo funciona correctamente.
Si utilizo la rutina que posteo a continuacion, solo funciona la primera vez, las siguientes parece como si el ftp utilizase cache, ya que siempre me hace get de la primera imagen. Alguien ve donde estoy fallando?
Código Delphi
[-]function FtpDownloadFile(strHost, strUser, strPwd: string;
Port: Integer; ftpDir, ftpFile, TargetFile: string; ProgressBar: TProgressBar; lbl : TLabel): Boolean;
function FmtFileSize(Size: Integer): string;
begin
if Size >= $F4240 then
Result := Format('%.2f', [Size / $F4240]) + ' Mb'
else
if Size < 1000 then
Result := IntToStr(Size) + ' bytes'
else
Result := Format('%.2f', [Size / 1000]) + ' Kb';
end;
const
READ_BUFFERSIZE = 4096; var
hNet, hFTP, hFile: HINTERNET;
buffer: array[0..READ_BUFFERSIZE - 1] of Char;
bufsize, dwBytesRead, fileSize: DWORD;
sRec: TWin32FindData;
strStatus: string;
LocalFile: file;
bSuccess: Boolean;
begin
Result := False;
DeleteFile( PChar(TargetFile));
hNet := InternetOpen('Program_Name', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if hNet = nil then
begin
ShowMessage('Unable to get access to WinInet.Dll');
Exit;
end;
hFTP := InternetConnect(hNet, PChar(strHost), port, PChar(StrUser), PChar(strPwd), INTERNET_SERVICE_FTP, 0, 0);
if hFTP = nil then
begin
InternetCloseHandle(hNet);
ShowMessage(Format('Host "%s" is not available',[strHost]));
Exit;
end;
if ( ftpDir = '') then bSuccess := true
else bSuccess := FtpSetCurrentDirectory(hFTP, PChar(ftpDir));
if not bSuccess then
begin
InternetCloseHandle(hFTP);
InternetCloseHandle(hNet);
ShowMessage(Format('Cannot set directory to %s.',[ftpDir]));
Exit;
end;
if FtpFindFirstFile(hFTP, PChar(ftpFile), sRec, 0, 0) <> nil then
begin
fileSize := sRec.nFileSizeLow;
end else
begin
InternetCloseHandle(hFTP);
InternetCloseHandle(hNet);
ShowMessage(Format('Cannot find file ',[ftpFile]));
Exit;
end;
hFile := FtpOpenFile(hFTP, PChar(ftpFile), GENERIC_READ, FTP_TRANSFER_TYPE_BINARY, 0);
if hFile = nil then
begin
InternetCloseHandle(hFTP);
InternetCloseHandle(hNet);
Exit;
end;
AssignFile(LocalFile, TargetFile);
{$i-}
Rewrite(LocalFile, 1);
{$i+}
if IOResult <> 0 then
begin
InternetCloseHandle(hFile);
InternetCloseHandle(hFTP);
InternetCloseHandle(hNet);
Exit;
end;
dwBytesRead := 0;
bufsize := READ_BUFFERSIZE;
while (bufsize > 0) do
begin
Application.ProcessMessages;
if not InternetReadFile(hFile,
@buffer, READ_BUFFERSIZE, bufsize) then Break;
if (bufsize > 0) and (bufsize <= READ_BUFFERSIZE) then
BlockWrite(LocalFile, buffer, bufsize);
dwBytesRead := dwBytesRead + bufsize;
if not ( ProgressBar = nil) then
ProgressBar.Position := Round(dwBytesRead * 100 / fileSize);
if not ( lbl = nil) then
lbl.Caption := Format('%s of %s / %d %%',[FmtFileSize(dwBytesRead),FmtFileSize(fileSize) ,ProgressBar.Position]);
end;
CloseFile(LocalFile);
InternetCloseHandle(hFile);
InternetCloseHandle(hFTP);
InternetCloseHandle(hNet);
Result := True;
end;