Delphi WinInet带进度下载Http文件


本文整理自网络,侵删。

 function FileSize(SizeInBytes: dword): string;  
const 
  Formats: array[0..3] of string =  (' Bytes', ' KB', ' MB', ' GB');  
  FormatSpecifier: array[Boolean] of string = ('%n', '%.2n');  
var  
  iLoop: integer;  
  TempSize: Real;  
begin  
  iLoop := -1;  
  TempSize := SizeInBytes;  
  while (iLoop <= 3) do 
  begin  
    TempSize := TempSize / 1024;  
    inc(iLoop);  
    if Trunc(TempSize) = 0 then  
    begin  
      TempSize := TempSize * 1024;  
      Break;  
    end;  
  end;  
  Result := Format(FormatSpecifier[((Frac(TempSize)*10) > 1)], [TempSize]);  
  if Copy(Result, Length(Result) - 2, 3) = '.00' then  
    Result := Copy(Result, 1, Length(Result) - 3);  
  Result := Result + Formats[iLoop];  
end;  
 
function ExtractURLSite(FileName: string): string;  
begin  
  Result := Copy(FileName, 1, Pos('/', FileName) - 1);  
end;  
 
function ExtractURLPath(FileName: string): string;  
begin  
  Result := Copy(FileName, Pos('/', FileName), Length(FileName) - Pos('/', FileName) + 1);  
end;  
 
function Split(Input: string; Deliminator: string; Index: integer): string;  
var  
  StringLoop, StringCount: integer;  
  Buffer: string;  
begin  
  Buffer := '';  
  if Index < 1 then Exit;  
  StringCount := 0;  
  StringLoop := 1;  
  while (StringLoop <= Length(Input)) do 
  begin  
    if (Copy(Input, StringLoop, Length(Deliminator)) = Deliminator) then  
    begin  
      Inc(StringLoop, Length(Deliminator) - 1);  
      Inc(StringCount);  
      if StringCount = Index then  
      begin  
        Result := Buffer;  
        Exit;  
      end  
      else 
      begin  
        Buffer := '';  
      end;  
    end  
    else 
    begin  
      Buffer := Buffer + Copy(Input, StringLoop, 1);  
    end;  
    Inc(StringLoop, 1);  
  end;  
  Inc(StringCount);  
  if StringCount < Index then Buffer := '';  
  Result := Buffer;  
end;  
 
function ThreadProc(lpParam: Pointer): DWORD; stdcall;  
const 
  BufferSize = 1024;  
  Agent = 'Internet Explorer 9.9';  
var  
  Session, Connect, Resource, OpenUrl: HINTERNET;  
  Buffer: array[1..BufferSize] of Byte;  
  BufferLen: DWORD;  
  f: File;  
  FileSize, ReseRved, _SizeOf, ReadSize:DWORD;  
  UrlFile, FileName, Site, URL, Location: string;  
begin  
 FileName:= 'c:\x.exe';  
 Result:=0;  
 UrlFile := 'http://xxxxxxxxxxxx/xxxxxxx.exe';  
 Location := Split(UrlFile, '://', 2);  
 URL := ExtractURLPath(Location);  
 Site := ExtractURLSite(Location);  
 Session := InternetOpenA(PAnsiChar(Agent), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);  
 if Assigned(Session) then  
 begin  
  try 
   Connect := InternetConnectA(Session, PAnsiChar(Site), INTERNET_DEFAULT_HTTP_PORT, nil, nil, INTERNET_SERVICE_HTTP, 0, 0);  
   if  Assigned(Connect) then  
   begin  
    try 
     Resource := HttpOpenRequestA(Connect, 'HEAD', PChar(URL), nil, nil, nil, 0, 0);  
     if Assigned(Resource) then  
     begin  
      try 
       if HttpSendRequestA(Resource, nil, 0, nil, 0) then  
       begin  
        _SizeOf := SizeOf(FileSize); //在下面api里直接用SizeOf 就说类型不一样- -!  
        ReseRved := 0;  
        FileSize := 0;  
        ReadSize := 0;  
        if (HttpQueryInfoA(Resource, HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @FileSize, _SizeOf, ReseRved)) and (FileSize <> 0) then  
        begin  
         try 
          OpenUrl := InternetOpenUrlA(Session, PAnsiChar(UrlFile),  nil,0,0,0);  
          if Assigned(OpenUrl) then  
          begin  
          try 
           AssignFile(f, FileName);  
           Rewrite(f,1);  
           repeat  
            if InternetReadFile(OpenUrl, @Buffer,SizeOf(Buffer), BufferLen) then  
            begin  
             BlockWrite(f, Buffer, BufferLen);  
             Inc(ReadSize,BufferLen);  
             Form1.SkinGauge.Value:=ReadSize;  
            end;  
           until BufferLen = 0;  
           CloseFile(f);  
          finally  
            InternetCloseHandle(OpenUrl)  
          end  
          end;  
         finally  
         end;  
        end;  
       end;  
      finally  
       InternetCloseHandle(Resource)  
      end;  
     end;  
    finally  
     InternetCloseHandle(Connect)  
    end;  
   end;  
  finally  
   InternetCloseHandle(Session)  
  end;  
 end;  
end;  
 
procedure TForm1.Button1Click(Sender: TObject);  
var  
  ID: DWORD;  
begin  
 CreateThread(nil, 0, @ThreadProc, nil, 0, ID);  
end; 

相关阅读 >>

Delphi 数据异或校验

Delphi richedit的内容保存为图片

Delphi 自定�x分割字符串

Delphi 隐藏进程的单元 unit hideprocess.pas

Delphi ioutils单元查找文件夹高级功能

Delphi 取系统临时路径

Delphi webbrowser1遍历网页中的图片

Delphi 倒计时源码

Delphi deletefile 删除文件

Delphi yesterday、today、tomorrow - 昨天、今天、明天

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...