İnternetten download edeceğim dosyanın boyutunu nasıl öğrenebilirim?Bununla ilgili bi kod buldum ama bi türlü çalıştırmmayı başaramadım.her seferinde dosya boyutunu 0 olarak gösteriyor...
bulduğum kod şu:
Kod: Tümünü seç
function GetInetFileSize(const FileURL: String): LongInt;
var
hInternetSession, hInternetConnect, hHttpOpenRequest : HINTERNET;
lpszAgent, lpszServerName, lpszObjectName, lpszReferrer, lplpszAcceptTypes : PChar;
lpdwBufferLength, lpdwReserved : DWORD;
lpvBuffer : Array[0..1024] Of Char;
begin
//Değişkenler ayarlanıyor
lpszAgent := PChar(ExtractFileName(Application.ExeName));
lpszServerName := PChar(GetParsedURL(FileURL,0));
lpszObjectName := PChar(GetParsedURL(FileURL,1));
lpszReferrer := PChar('');
lplpszAcceptTypes := PChar('Accept: */*');
lpdwBufferLength := 1024;
lpdwReserved := 0;
try
hInternetSession := InternetOpen(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
hInternetConnect := InternetConnect(hInternetSession, lpszServerName, INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
hHttpOpenRequest := HttpOpenRequest(hInternetConnect, 'HEAD', lpszObjectName, 'HTTP/1.1', lpszReferrer, @lplpszAcceptTypes, INTERNET_FLAG_RELOAD, 0);
HttpSendRequest(hHttpOpenRequest, nil, 0, 0, 0);
HttpQueryInfo(hHttpOpenRequest, HTTP_QUERY_CONTENT_LENGTH, @lpvBuffer, lpdwBufferLength, lpdwReserved);
if lpdwBufferLength > 0 then
begin
Result := StrToInt(Copy(lpvBuffer,1,lpdwBufferLength));
end
else
begin
Result := 0;
end;
except
Result := 0;
end;
except
Result := 0;
end;
if hInternetSession <> nil then
begin
InternetCloseHandle(hInternetSession);
end;
end;
Kod: Tümünü seç
toplambyte:= GetInetFileSize(fileurl);
http://www.yazilimgrubu.com/kod.php?sayfa2=5&no=119
Kod: Tümünü seç
İnternetteki Dosyanın Boyutu (Component Olmadan)
WinInet Unit'ini kullanarak component kullanmadan
internet üzerindeki bir dosyanın boyutunu alabilirsiniz.
Aşağıdaki örnek uygulama için formun üzerine
1 adet standart buton koyun.
unit Unit1;
interface
uses
Windows, Classes, Controls, StdCtrls, Forms, SysUtils, Dialogs, WinInet;
{
, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinInet, StdCtrls
}
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetParsedURL(const URL: String; const ResultType : SmallInt): String;
var
Pos1 : SmallInt;
Temp1 : String;
begin
Result := '';
Temp1 := Trim(URL);
Temp1 := StringReplace(Temp1,'\','/',[rfReplaceAll]);
if Length(Temp1) > 0 then
begin
//---
Pos1 := Pos('://',Temp1);
if Pos1 > 0 then
begin
//HTTP Yazılmış
Pos1 := Pos1 + 3;
Temp1 := Trim(Copy(Temp1,Pos1,Length(URL)));
end;
Pos1 := Pos('/',Temp1);
case ResultType of
0: //Server
begin
if Pos1 > 0 then
begin
Temp1 := Trim(Copy(Temp1,1,(Pos1-1)));
end;
Result := Temp1;
end;
1: //Dosya
begin
if Pos1 > 0 then
begin
Temp1 := Copy(Temp1,Pos1,Length(Temp1));
Result := Temp1;
end
else
begin
Result := '/';
end;
end;
end;
//---
end;
end;
function GetInetFileSize(const FileURL: String): LongInt;
var
hInternetSession, hInternetConnect, hHttpOpenRequest : HINTERNET;
lpszAgent, lpszServerName, lpszObjectName, lpszReferrer, lplpszAcceptTypes : PChar;
lpdwBufferLength, lpdwReserved : DWORD;
lpvBuffer : Array[0..1024] Of Char;
begin
//Değişkenler ayarlanıyor
lpszAgent := PChar(ExtractFileName(Application.ExeName));
lpszServerName := PChar(GetParsedURL(FileURL,0));
lpszObjectName := PChar(GetParsedURL(FileURL,1));
lpszReferrer := PChar('');
lplpszAcceptTypes := PChar('Accept: */*');
lpdwBufferLength := 1024;
lpdwReserved := 0;
try
hInternetSession := InternetOpen(lpszAgent, INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
try
hInternetConnect := InternetConnect(hInternetSession, lpszServerName, INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);
hHttpOpenRequest := HttpOpenRequest(hInternetConnect, 'HEAD', lpszObjectName, 'HTTP/1.1', lpszReferrer, @lplpszAcceptTypes, INTERNET_FLAG_RELOAD, 0);
HttpSendRequest(hHttpOpenRequest, nil, 0, 0, 0);
HttpQueryInfo(hHttpOpenRequest, HTTP_QUERY_CONTENT_LENGTH, @lpvBuffer, lpdwBufferLength, lpdwReserved);
if lpdwBufferLength > 0 then
begin
Result := StrToInt(Copy(lpvBuffer,1,lpdwBufferLength));
end
else
begin
Result := 0;
end;
except
Result := 0;
end;
except
Result := 0;
end;
if hInternetSession <> nil then
begin
InternetCloseHandle(hInternetSession);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
//KULLANIMI
ShowMessage('Dosya Boyutu: ' + IntToStr(GetInetFileSize('http://download.microsoft.com/download/d/c/3/dc37439a-172b-4f20-beac-bab52cdd38bc/Windows-KB833330-ENU.exe')));
end;
end.