[DELPHI] - Download Files
UPDATED WITH NEW METHOD
Code:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, WinInet;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function GetInetFile(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);
repeat
InternetReadFile(hURL, @Buffer,
SizeOf(Buffer), BufferLen);
BlockWrite(f, Buffer, BufferLen)
until BufferLen = 0;
CloseFile(f);
Result:=True;
finally
InternetCloseHandle(hURL)
end
finally
InternetCloseHandle(hSession)
end
end;
procedure TForm1.Button1Click(Sender: TObject);
var
InternetFile,LocalFile: string;
begin
InternetFile:='http://www.vbforums.com/images/icons/icon6.gif';
LocalFile:='D:/Cool.gif';
if GetInetFile(InternetFile,LocalFile)=True then
ShowMessage('download')
else
ShowMessage('Can not download the files');
end;
end.
Code:
Uses
URLMon, ShellApi;
function DownloadFile(SourceFile, DestFile: string): Boolean;
begin
try
Result := UrlDownloadToFile(nil, PChar(SourceFile), PChar(DestFile), 0, nil) = 0;
except
Result := False;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
const
// URL Location
SourceFile = 'http://www.vbforums.com/member.php?u=45760';
// Where to save the file
DestFile = 'C:\Image.gif';
begin
if DownloadFile(SourceFile, DestFile) then
ShowMessage('Download succesful!');
else
ShowMessage('Error while downloading ' + SourceFile)
end;
Re: [DELPHI] - Download Files
First, thx for posting this code...
but i have a problem using it..When i run the program they give me this error:
[Error] Unit1.pas(25): Undeclared identifier: 'HInternet'
Re: [DELPHI] - Download Files
did you include the ShellAPI in the uses clause?
Re: [DELPHI] - Download Files
If it doesnt work ill rebuild an example project for you
Re: [DELPHI] - Download Files
Quote:
Originally Posted by Madboy
did you include the ShellAPI in the uses clause?
No, I'm Sorry i am still learning Delphi (I was a VB programmer) and i start working in Delphi in almost 2 months.
Do you mean something like that
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ShellAPI;
Cause this didn't work :confused:
Edit:Oh, do you mean the ShellExecute API..
No i didn't..Can you tell me how to add it :confused:
Re: [DELPHI] - Download Files
well the ShellAPI was in the right place, ill have a look in a bit, i think the HInternet may need to be stored as a integer or something, ill have a look through
Re: [DELPHI] - Download Files
Try the code in the first post, i updated it, tested, and it works ok for me :)
PS: The WinInet needed declaring in the Uses section, my mistake it wasnt ShellAPI :eek:
Re: [DELPHI] - Download Files
Quote:
Originally Posted by Madboy
Try the code in the first post, i updated it, tested, and it works ok for me :)
PS: The WinInet needed declaring in the Uses section, my mistake it wasnt ShellAPI :eek:
Allright!! :)
Thx alot man :D
Re: [DELPHI] - Download Files
If you get stuck im happy to try and help again, as i think im the only Delphi programmer here. Though VisualAd and CoronaBeer have helped in the past, they are not full timer Delphiers :)
Re: [DELPHI] - Download Files
where is the download progress?!
bad code.
Re: [DELPHI] - Download Files
Quote:
Originally Posted by
kakablack
where is the download progress?!
bad code.
If you don't like the code why don't you just write it yourself?