|
[ anon68680 @ 22.09.2005. 16:53 ] @
| Code:
function TForm1.DownloadFile
(const fileURL, FileName: String): boolean;
const BufferSize = 1024;
var
hSession, hURL: HInternet;
Buffer: array[1..BufferSize] of Byte;
BufferLen: DWORD;
f: File;
sAppName: string;
begin
Result:=False;
sAppName := ExtractFileName(Application.ExeName);
hSession := InternetOpen(PChar(sAppName),
INTERNET_OPEN_TYPE_PRECONFIG,
nil, nil, 0);
try
hURL := InternetOpenURL(hSession,
PChar(fileURL),
nil,0,0,0);
try
AssignFile(f, FileName);
Rewrite(f,1);
Gauge1.MaxValue:=BufferLen;
repeat
InternetReadFile(hURL, @Buffer,
SizeOf(Buffer), BufferLen);
BlockWrite(f, Buffer, BufferLen);
Gauge1.Progress:=Gauge1.Progress+1;
until BufferLen = 0;
CloseFile(f);
Result:=True;
finally
InternetCloseHandle(hURL)
end
finally
InternetCloseHandle(hSession)
end
end;
naisao sam na ovaj cod kada sam trazio neshto sto ce mi omoguciti download nekog filea sa interneta a da pritom mogu prikaziti progress tog downloadovanja u nekom progressbaru. problem je u tome sto se pri startovanju funkcije program zaledi do zavrsetka downloada. Da li neko zna u cemu je problem? |
[ Srki_82 @ 22.09.2005. 22:46 ] @
Negde izmedju repeat until ubaci Application.ProcessMessages
[ reiser @ 22.09.2005. 22:47 ] @
Najbolje ispod Gauge1.Progress:=Gauge1.Progress+1; linije
[Ovu poruku je menjao reiser dana 22.09.2005. u 23:47 GMT+1]
[ anon68680 @ 22.09.2005. 23:45 ] @
noup idalje imam problema, posto se zavrshi download applikacija pocinje da koci i jos neke gluposti. da li mozda neko zna neku drugu funkciju koja ima progress mogucnost?
[ Srki_82 @ 23.09.2005. 08:17 ] @
Instaliraj Jedi-VCL komponente... tu imas sve i svasta, a izmedju ostalog i komponente za download.
[ anon68680 @ 23.09.2005. 23:19 ] @
pronasao sam download funkciju koja radi:
Code:
function DownloadFile(const Url: string; destFName: string): string;
var
NetHandle: HINTERNET;
UrlHandle: HINTERNET;
Buffer: array[0..1024] of Char;
BytesRead: dWord;
f: File;
i: longint;
begin
Result := '';
NetHandle := InternetOpen('Delphi ', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(NetHandle) then begin
UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(UrlHandle) then begin
FillChar(Buffer, SizeOf(Buffer), 0);
AssignFile(f, destFName);
Rewrite(f,1);
i:=0;
form3.Gauge2.Maxvalue:=bytesread;
repeat
Result := Result + Buffer;
FillChar(Buffer, SizeOf(Buffer), 0);
InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
i:=myFileSize(destFName);
form3.Gauge2.Progress:=i;
BlockWrite(f, Buffer, BytesRead);
Application.ProcessMessages;
until BytesRead = 0;
CloseFile(f);
InternetCloseHandle(UrlHandle);
end;
end;
end;
sada kao sto vidite form3.Gauge2.Maxvalue:=bytesread; treba da se zameni sa velicinom filea na serveru odakle se skida. ali kako?
[Ovu poruku je menjao krza dana 24.09.2005. u 00:35 GMT+1]
[ Srki_82 @ 23.09.2005. 23:42 ] @
Pogledaj kako sam napisao TAutoUpdate komponentu za Delphi 7... mozda ti pomogne.
http://www.elitesecurity.org/tema/136194-ZeusGames-AutoUpdate
[ ivanho @ 24.09.2005. 07:48 ] @
Code:
function DownloadFile(strUrlFile, strDestFile: string;
comLabel: TLabel = Nil;
comProgressBar: TProgressBar = Nil
): boolean;
var
handleNet: HINTERNET;
handleUrl: HINTERNET;
arrchBuffer: Array[0..1024] Of Char;
dwFileSize, dwReserved, dwBytesRead: dWord;
intNumByte: Integer;
strReadFile: String;
begin
Result := false;
try
handleNet := InternetOpen(PChar(Application.Title), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
if Assigned(handleNet) then
begin
handleUrl := InternetOpenUrl(handleNet, PChar(strUrlFile), nil, 0, INTERNET_FLAG_RELOAD, 0);
if Assigned(handleUrl) then
begin
// koliko je velik fajl
arrchBuffer := '';
dwFileSize := SizeOf(arrchBuffer);
dwReserved := 0;
If HttpQueryInfo(handleUrl, HTTP_QUERY_CONTENT_LENGTH, @arrchBuffer, dwFileSize, dwReserved) Then
dwFileSize := StrToIntDef(arrchBuffer, -1);
if comLabel <> nil Then
comLabel.Caption := '0 od 0 bytes';
if comProgressBar <> nil then
begin
comProgressBar.Min:=0;
comProgressBar.Max:=dwFileSize;
comProgressBar.Position:=0;
end;
FillChar(arrchBuffer, SizeOf(arrchBuffer), 0);
repeat
FillChar(arrchBuffer, SizeOf(arrchBuffer), 0);
InternetReadFile(handleUrl, @arrchBuffer, SizeOf(arrchBuffer), dwBytesRead);
for intNumByte := 0 To dwBytesRead - 1 Do
strReadFile := Concat(strReadFile, arrchBuffer[intNumByte]);
if comProgressBar <> nil then
comProgressBar.Position := Form1.ProgressBar1.Position + StrToInt(IntToStr(dwBytesRead));
if comLabel <> nil then
comLabel.Caption := FormatFloat('0,000', Length(strReadFile)) + ' od ' + FormatFloat('0,000', dwFileSize) + ' bytes';
Application.ProcessMessages;
until dwBytesRead = 0;
If Length(strReadFile) > 0 Then
begin
If FileExists(strDestFile) Then
DeleteFile(PChar(strDestFile));
with TFileStream.Create(strDestFile, fmCreate) Do
try
Write(strReadFile[1], Length(strReadFile));
finally
Free;
end;
result := True;
end
else
result := False;
end;
end;
finally
InternetCloseHandle(handleUrl);
InternetCloseHandle(handleNet);
end;
end;
[ anon68680 @ 24.09.2005. 12:00 ] @
noup ni ova ne radi... :(
kaze da je file veliki nekih 1000bytes a ustvari je bar 200 000...
[ Srki_82 @ 24.09.2005. 12:34 ] @
Kao sto rekoh... pogledaj kod za TAutoUpdate i sve ce ti biti jasno. Koristi se TDownLoadURL komponenta koja ide sa Delphi 7.
[ anon68680 @ 24.09.2005. 12:42 ] @
da upravo sam je skino samo sto ne znam kako se koristi. ima dve onprogress eventa ne znam koji je koji...
[ Srki_82 @ 24.09.2005. 12:47 ] @
Znam da ne znas kako se koristi... pogledaj kako se koristi TDownLoadURL komponenta. To je ono sto tebi treba.
[ anon68680 @ 24.09.2005. 13:22 ] @
da mislim da sam skuzio vishe manje. ali ne znam zaglavio sam se. evo sta sam uradio:
Code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtActns, Wininet, ComCtrls, StdCtrls, AutoUpdate;
type
TForm1 = class(TForm)
AutoUpdate1: TAutoUpdate;
ProgressBar1: TProgressBar;
Button1: TButton;
procedure AutoUpdate1DownLoadUpdateDataProgress(Sender: TAutoUpdate;
Progress, ProgressMax: Cardinal; var Cancel: Boolean);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.AutoUpdate1DownLoadUpdateDataProgress(Sender: TAutoUpdate;
Progress, ProgressMax: Cardinal; var Cancel: Boolean);
begin
ProgressBar1.Max:=progressmax;
ProgressBar1.Position:=progress;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AutoUpdate1.UpdateAddress:='http://users.cjb.net/netwalker/';
AutoUpdate1.UpdateDataFile:='chbook.ini';
AutoUpdate1.DownLoadUpdateData;
end;
end.
e sad ovde u eventu button1click treba da odredim verovatno gde file da ide a ne znam kako?
[ Srki_82 @ 24.09.2005. 17:35 ] @
Hehe... nemoj da koristis TAutoUpdate komponentu za obicno skidanje fajla. Koristi samo TDownLoadURL komponentu. Imas help za nju u Delphi-u.
Ako ti treba bas autoupdate funkcija za tvoj program posalji mi pm pa cu ti objasniti kako se koristi.
[ Nemanja Avramović @ 27.09.2005. 17:16 ] @
ako nisi resio problem, evo:
dodas u uses URLMon
i onda ubacis sledecu funkciju:
Code:
function DownloadFile(source, destination: string): boolean;
var
SourceFile, LocalFile: string;
begin
SourceFile := source;
LocalFile := destination;
if URLDownloadToFile(nil, PChar(SourceFile), PChar(LocalFile), 0, nil) = 0 then
result := true
else
result := false;
end;
Pa pozivas:
Code:
if not DownloadFile('http://www.sajt.com/path/to/file.ext','c:\file.ext') then
showmessage('Ne mogu da skinem fajl')
else
uradinestosafajlom; // ;)
end;
p.s. mislim da sa ovim ne mozes da 'uhvatis' progress kod downloada...
[ anon68680 @ 27.09.2005. 18:45 ] @
hvala ali meni treba download sa progressom a ovu funkciju vec imam 
Copyright (C) 2001-2025 by www.elitesecurity.org. All rights reserved.
|