Use windows api to download file in delphi
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, WinInet, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} function DownloadToString(const Url: string): string; var NetHandle: HINTERNET; UrlHandle: HINTERNET; Buffer: array[0..1024] of Char; BytesRead: dWord; begin Result := ''; NetHandle := InternetOpen('Delphi 5.x', 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 { UrlHandle valid? Proceed with download } begin FillChar(Buffer, SizeOf(Buffer), 0); repeat Result := Result + Buffer; FillChar(Buffer, SizeOf(Buffer), 0); InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead); until BytesRead = 0; InternetCloseHandle(UrlHandle); end else { UrlHandle is not valid. Raise an exception. } raise Exception.CreateFmt('Cannot open URL %s', [Url]); InternetCloseHandle(NetHandle); end else { NetHandle is not valid. Raise an exception } raise Exception.Create('Unable to initialize Wininet'); end; function DownloadToFile(const Url, LFile: string): Boolean; var NetHandle: HINTERNET; UrlHandle: HINTERNET; Buffer: array[0..1024] of Char; BytesRead: dWord; LFileStream: TFileStream; begin Result := False; NetHandle := InternetOpen('Delphi 5.x', 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 { UrlHandle valid? Proceed with download } begin LFileStream := TFileStream.Create(LFile, fmCreate); FillChar(Buffer, SizeOf(Buffer), 0); repeat // Result := Result + Buffer; FillChar(Buffer, SizeOf(Buffer), 0); InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead); LFileStream.Write(Buffer, BytesRead); until BytesRead = 0; LFileStream.Free; InternetCloseHandle(UrlHandle); end else { UrlHandle is not valid. Raise an exception. } raise Exception.CreateFmt('Cannot open URL %s', [Url]); InternetCloseHandle(NetHandle); end else { NetHandle is not valid. Raise an exception } raise Exception.Create('Unable to initialize Wininet'); end; procedure TForm1.Button1Click(Sender: TObject); begin Memo1.Lines.Text := DownloadToString('http://www.mailsextractor.com/') end; procedure TForm1.Button2Click(Sender: TObject); begin DownloadToFile('http://www.mailsextractor.com/mailsextractor.exe', 'mailsextractor.exe'); end; end. |
above is the code