Cześć wszystkim ..
Zaobserwowałem dziwne zdarzenie podczas pobierania pliku za pomoca mojego kodu ...
Gdy zaczynam sciągac plik np: 3000kb od początku i pobieram np 10000b i wstrzymuje pobieranie - to jest ok ... ale gdy wznawiam pobieranie to program pobiera te wspomniane 10000b od początku dopiero gdy pobierze powyzej 1000kb odpala sie petla i wyświetla mi opis : pobieranie 10005kb itp.. ?? dziwne - a jak bym miał sciagniete 300mb ?? - za kazdym razem od początku ?? myślalem ze to przez INTERNET_FLAG_RELOAD ale ja usunelem i obecnie linia wyglada ...
hConnection := InternetOpenURL(hSession, PChar(sessionInfo.Link), nil, 0,0,0);
moze trzeba zastosowac jakas flage ??
zapisywanie dziala bez zarzutu ... wszystko dziala poza tym malym drazniącym incydentem ...
Pomocy ....
kod caly -
unit threadHttpGet;
// Library: threadHttpGet.pas
// ------------------------------------------------------------
// Connecting to internet and download all or parts of files.
// ------------------------------------------------------------
interface
uses
Windows, Messages, SysUtils, Variants, Classes;
type
// Property for progressBar
TProgressInfo = procedure (AMax, APos: Int64) of object;
// Print all info
THttpEvent = procedure (AEvents: string) of object;
// Config
TOnData = record
Link: string;
theFile, tmpFile, tmpFileD: string;
tmpExist: boolean;
end;
// Main thread ...
TDownloadFile = class(TThread)
private
sessionInfo: TOnData;
FOnEvent: THttpEvent;
FOnProgressInfo: TProgressInfo;
protected
procedure Execute; override;
public
Abort: boolean; // Abort downloading
isBusy: boolean; // We are in loop this is true outside has a false
doneFile: boolean; // When data file is same like httpsize
resumeFile: boolean;
constructor create (ALink, lFile: string; ProgressInfo: TProgressInfo; Event: ThttpEvent);
end;
const
BUFF_SIZE = 6144;
_APP_NAME_ = 'Fast Downloader 1.0';
var
//localSize: int64 = 0;
httpLen: int64 = 0;
implementation
uses
WinInet;
// --
constructor TDownloadFile.Create (ALink, lFile: string; ProgressInfo: TProgressInfo; Event: ThttpEvent);
begin
FOnProgressInfo := ProgressInfo;
FOnEvent := Event;
Abort := False;
isBusy := False;
doneFile := false;
resumeFile := false;
FreeOnTerminate := true;
with sessionInfo do
begin
Link := ALink;
theFile := lFile;
tmpFile := format ('%s%s.partA', [IncludeTrailingPathDelimiter (ExtractFileDir (lFile)), ExtractFileName (lFile)]);
tmpExist := FileExists (tmpFile);
end;
inherited Create (true);
end;
procedure TDownloadFile.Execute;
var
bytesRead: array [1..5120] of byte;
hConnection, hSession : HINTERNET;
nowRead, localSize: DWORD;
tmpAFile: TFileStream;
totalRead: int64;
///////////////////
////////////////
begin
if sessionInfo.tmpExist then
begin
tmpAFile := TFileStream.Create(sessionInfo.tmpFile, fmOpenReadWrite or fmShareExclusive);
localSize := tmpAFile.Size;
tmpAFile.Seek(tmpAFile.Size, soFromBeginning);
//resumeFile := true;
end else
begin
tmpAFile := TFileStream.Create(sessionInfo.tmpFile, fmCreate);
localSize := tmpAFile.Size;
end;
FOnEvent ('Connecting with '+sessionInfo.Link);
hSession := InternetOpen(_APP_NAME_, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
// tu moze byc blad ??
hConnection := InternetOpenURL(hSession, PChar(sessionInfo.Link), nil, 0, 0, 0);
try
{// code = 200
if resumeFile then
begin end;
FOnEvent ('Wznawiamy pobieranie pliku'); } {and (InternetQueryDataAvailable (hConnection, localSize, 0, 0)) }
if (localSize > 0) then
// tu moze byc blad ??
InternetSetFilePointer(hConnection, localSize, nil, 0,0);
//sleep (100);
FOnEvent ('Oczekiwanie na odpowiedz ...');
totalRead := 0;
FOnEvent ('Pobieranie pliku ...');
// jesli przerwe pobieranie na 10000b i je wznowie to jeszcze raz pobiera te 10000b i
// przechodzi ponizej??
repeat
InternetReadFile(hConnection, @bytesRead, SizeOf(bytesRead), nowRead);
// A moze to dlatego ze tego nie dodaje ??
// InternetQueryDataAvailable(inURL,rozmiar, 0, 0);
tmpAFile.WriteBuffer(bytesRead, nowRead);
inc (totalRead, nowRead);
sleep (100);
if(tmpAFile.Size div 1024) < 1024 then FOnEvent ( format ('Pobieranie %d KB', [(tmpAFile.Size div 1024)]) )
else FOnEvent ( format ('Pobieranie %.2f MB', [(tmpAFile.Size / (1024*1024))]) );
until (nowRead = 0) or (Abort = true);
// Option when user press abort or not internet
if Abort then
begin
FOnEvent ('User Aborted at 34956'); sleep (1100);
FOnEvent ('Pobrano: '+intToStr(TotalRead div 1024)+' KB');
end;
finally
InternetCloseHandle(hConnection);
end;
finally
InternetCloseHandle(hSession);
tmpAFile.Free; // close file handle
end;
end;
end.