-
Jun 21st, 2005, 12:09 PM
#1
Thread Starter
Supreme User
[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;
Last edited by Madboy; Jul 22nd, 2005 at 02:59 PM.
-
Jul 21st, 2005, 10:21 AM
#2
Lively Member
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'
-
Jul 21st, 2005, 11:09 AM
#3
Thread Starter
Supreme User
Re: [DELPHI] - Download Files
did you include the ShellAPI in the uses clause?
-
Jul 21st, 2005, 11:15 AM
#4
Thread Starter
Supreme User
Re: [DELPHI] - Download Files
If it doesnt work ill rebuild an example project for you
-
Jul 21st, 2005, 11:23 AM
#5
Lively Member
Re: [DELPHI] - Download Files
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
Edit:Oh, do you mean the ShellExecute API..
No i didn't..Can you tell me how to add it
Last edited by Shady Soft; Jul 21st, 2005 at 03:30 PM.
-
Jul 21st, 2005, 03:35 PM
#6
Thread Starter
Supreme User
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
-
Jul 21st, 2005, 03:58 PM
#7
Thread Starter
Supreme User
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
-
Jul 22nd, 2005, 01:19 AM
#8
Lively Member
Re: [DELPHI] - Download Files
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
Allright!!
Thx alot man
-
Jul 22nd, 2005, 03:11 AM
#9
Thread Starter
Supreme User
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
-
Dec 19th, 2011, 06:15 AM
#10
Addicted Member
Re: [DELPHI] - Download Files
where is the download progress?!
bad code.
-
Dec 21st, 2011, 08:33 AM
#11
Re: [DELPHI] - Download Files
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?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|