1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78:
| function DownloadFile(const Url: string;Target:String; sd:string): bool; var NetHandle: HINTERNET; UrlHandle: HINTERNET; Buffer: array[0..1024] of Char; BytesRead: dWord; TargetFile:File; dwBufLen, dwIndex: DWord; Buf: Pointer; fsize,GotData,Position:real; Temp,Code:Integer; begin ProcessMessages(WINDOW); NetHandle := InternetOpen(INETNAME, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0); ProcessMessages(WINDOW); UpdateWindow(WINDOW); if Assigned(NetHandle) then begin
ProcessMessages(WINDOW); UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0); ProcessMessages(WINDOW); UpdateWindow(WINDOW);
if Assigned(UrlHandle) then begin AssignFile(TargetFile, Target); Rewrite(TargetFile, 1); dwIndex := 0; dwBufLen:=1024; GotData:=0; GetMem(Buf, dwBufLen);
HttpQueryInfo(UrlHandle, HTTP_QUERY_CONTENT_LENGTH, Buf, dwBufLen, dwIndex); val(StrPas(Buf),Temp,Code); fSize:=Temp; SendMessage(Progress, PBM_SETPOS, round(GotData), 0);
repeat
If EndDownload then begin InternetCloseHandle(UrlHandle); InternetCloseHandle(NetHandle); CloseFile(TargetFile); DeleteFile(PChar(Target)); result:=false; Exit; end;
ProcessMessages(WINDOW); InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead); UpdateWindow(WINDOW); BlockWrite(TargetFile, Buffer, BytesRead);
GotData:=GotData+BytesRead; Position:=PROGRESSMAX / fsize * GotData; SendMessage(Progress, PBM_SETPOS, round(Position), 0);
until BytesRead = 0;
CloseFile(TargetFile); ShellExecute(hInstance, 'open', PChar(Target), PChar(sd), nil, SW_ShowNormal);
InternetCloseHandle(UrlHandle); result:=true;
end else begin result:=false; MessageBox(WINDOW,CONNECTIONERROR,ERRORCAPTION,MB_APPLMODAL OR MB_ICONERROR OR MB_OK); end; InternetCloseHandle(NetHandle); end else begin MessageBox(WINDOW,CONNECTIONERROR,ERRORCAPTION,MB_APPLMODAL OR MB_ICONERROR OR MB_OK); result:=false; end; end; |