İnternetten download edeceğim dosyanın boyutunu öğrenme?

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
kahraman1285
Üye
Mesajlar: 360
Kayıt: 10 Nis 2006 09:07

İnternetten download edeceğim dosyanın boyutunu öğrenme?

Mesaj gönderen kahraman1285 »

Merhaba;

İ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;
kullanım sekli

Kod: Tümünü seç

toplambyte:= GetInetFileSize(fileurl);
bide aşağıdaki linkte bir örnek buldum ama o kodda her dosyanın boyutunu "20" olarak gösteriyor bi türlü işin içinden çıkamadım

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.
 


yardımlarınızı bekliyorum .teşekkürler.
kahraman1285
Üye
Mesajlar: 360
Kayıt: 10 Nis 2006 09:07

Mesaj gönderen kahraman1285 »

TidHttp componentini kullanarak çok kolay bi yöntemini http://www.delphipages.com rastladım.aynı sıkıntıyı yaşayan arkadaşlar bu kodu kullanabilir denedim...

Kod: Tümünü seç

var filesize:integer;

idHTTP1.Head(dosyanın bulunduğu adres);

FileSize := idHTTP1.Response.ContentLength;
Kullanıcı avatarı
Trial_Cooder
Üye
Mesajlar: 236
Kayıt: 17 Nis 2006 04:44

Mesaj gönderen Trial_Cooder »

O Kadar Uzun Koda Gerek Yok Sanırım

Kod: Tümünü seç

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;
Bu İşini Görür ;)
Cevapla