Mark Brady
asked on
How to download multiple files from web server to local folder and access ?
Gidday team !
I need my delphi app to download files from one folder on my server. I can write the code for a single file download but I need to download all files in this server folder into a local folder on the users pc. For eg:
'http://somewebsite.com/files/' // this would be my web server
'c:\application\files' // this is the local folder
Now my application needs to check the webserver folder (say every 5 minutes) for the file contents and compare them with the local file contents. If any file is missing on the server (but resides in the local folder) then application is to delete it from local folder.
At the same time, if any addition file is found on server but not on local machine then it should download it.
This application is a console app that uses an advertising clicker on the bottom of the form. Ads willd rotate and change every minute. The above information will help me to regularly check for new adverts to download and/or adds that have been removed.
Lastly, the downloaded files are '.jpg' files that my app will load into an image. Don't know how to load gif files(moving) yet so I will stick to still adds. Can this be done without downloading them ?
Thanks in advance
Elvin
I need my delphi app to download files from one folder on my server. I can write the code for a single file download but I need to download all files in this server folder into a local folder on the users pc. For eg:
'http://somewebsite.com/files/' // this would be my web server
'c:\application\files' // this is the local folder
Now my application needs to check the webserver folder (say every 5 minutes) for the file contents and compare them with the local file contents. If any file is missing on the server (but resides in the local folder) then application is to delete it from local folder.
At the same time, if any addition file is found on server but not on local machine then it should download it.
This application is a console app that uses an advertising clicker on the bottom of the form. Ads willd rotate and change every minute. The above information will help me to regularly check for new adverts to download and/or adds that have been removed.
Lastly, the downloaded files are '.jpg' files that my app will load into an image. Don't know how to load gif files(moving) yet so I will stick to still adds. Can this be done without downloading them ?
Thanks in advance
Elvin
ASKER
Actually I had a play with it and was able to DL some files from a directory by adding a txt file with the actual file URL's to the same folder on my server. The program downloads the text file and reads into a comboboxx then should run through and download each image listed in the combobox. Slight problem here. The text file comes down great and loads into combo. The first file downloads ok then it crashes with an EInOutError with message 'I/O error 1784'.
Here is my full code so far. Can you see what is causing this ?
unit Address;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus, jpeg, Wininet;
type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
Label1: TLabel;
Panel2: TPanel;
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
Edit2: TMenuItem;
AddContact1: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
Label2: TLabel;
Image2: TImage;
Image1: TImage;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Button1: TButton;
procedure Exit1Click(Sender: TObject);
procedure getadds(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(Applicatio n.ExeName) ;
hSession := InternetOpen(PChar(sAppNam e),
INTERNET_OPEN_TYPE_PRECONF IG,
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); // -------------Crashes on this line-----------//
FlashWindow(Application.Ha ndle,True) ;
until BufferLen = 0;
CloseFile(f);
Result:=True;
finally
InternetCloseHandle(hURL)
end
finally
InternetCloseHandle(hSessi on)
end
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
close;
end;
procedure TForm1.getadds(Sender: TObject);
var FileOnNet, LocalFileName, s: string;
i: integer;
begin
FileOnNet:=
'http://www.lfoband.com/adds/adds.txt';
LocalFileName:='c:\adds\ad ds.txt';
if GetInetFile(FileOnNet,Loca lFileName) =True then
combobox1.Items.LoadFromFi le(localfi lename)
else
begin
ShowMessage('Error in file download');
exit;
end;
combobox1.itemindex := -1;
for i:= 0 to combobox1.items.count-1 do
begin
combobox1.itemindex := combobox1.itemindex + 1;
FileOnNet:= combobox1.items[combobox1. itemindex] ;
LocalFileName:= 'c:\adds\' + (extractfilename(combobox1 .items[com bobox1.ite mindex])) ;
if GetInetFile(FileOnNet,Loca lFileName) =true then
showmessage('Sucess !')
else
ShowMessage('Error in file download');
end;
end;
end.
Here is my full code so far. Can you see what is causing this ?
unit Address;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus, jpeg, Wininet;
type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
Label1: TLabel;
Panel2: TPanel;
MainMenu1: TMainMenu;
File1: TMenuItem;
Exit1: TMenuItem;
Edit2: TMenuItem;
AddContact1: TMenuItem;
Help1: TMenuItem;
About1: TMenuItem;
Label2: TLabel;
Image2: TImage;
Image1: TImage;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Button1: TButton;
procedure Exit1Click(Sender: TObject);
procedure getadds(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(Applicatio
hSession := InternetOpen(PChar(sAppNam
INTERNET_OPEN_TYPE_PRECONF
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); // -------------Crashes on this line-----------//
FlashWindow(Application.Ha
until BufferLen = 0;
CloseFile(f);
Result:=True;
finally
InternetCloseHandle(hURL)
end
finally
InternetCloseHandle(hSessi
end
end;
procedure TForm1.Exit1Click(Sender: TObject);
begin
close;
end;
procedure TForm1.getadds(Sender: TObject);
var FileOnNet, LocalFileName, s: string;
i: integer;
begin
FileOnNet:=
'http://www.lfoband.com/adds/adds.txt';
LocalFileName:='c:\adds\ad
if GetInetFile(FileOnNet,Loca
combobox1.Items.LoadFromFi
else
begin
ShowMessage('Error in file download');
exit;
end;
combobox1.itemindex := -1;
for i:= 0 to combobox1.items.count-1 do
begin
combobox1.itemindex := combobox1.itemindex + 1;
FileOnNet:= combobox1.items[combobox1.
LocalFileName:= 'c:\adds\' + (extractfilename(combobox1
if GetInetFile(FileOnNet,Loca
showmessage('Sucess !')
else
ShowMessage('Error in file download');
end;
end;
end.
ASKER
One more thing. When I say the first file downloads ok (meaning the first add which is a jpg) it is visible on my hard drive but is 0 bytes so it is not being written properly. The txt file is being written propery.
Here is the contents of the txt file.......
http:\\www.lfoband.com\adds\banner.jpg
http:\\www.lfoband.com\adds\lfo.jpg
http:\\www.lfoband.com\adds\addcf.jpg
Here is the contents of the txt file.......
http:\\www.lfoband.com\adds\banner.jpg
http:\\www.lfoband.com\adds\lfo.jpg
http:\\www.lfoband.com\adds\addcf.jpg
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Hey thanks ciuly I'll try this code when I wake up ! I can't believe that Microcrap has so many limitations. Thanks for your help and I'll let you know how it goes.
Elvin
Elvin
ASKER
Where do I get IdBaseComponent, IdAntiFreezeBase and IdAntiFreeze from ?
I'm compiling this under delphi 5 but do have delphi studio.
Elvin
I'm compiling this under delphi 5 but do have delphi studio.
Elvin
install indy 9 from http://www.indyproject.org/Sockets/Download/Files/Indy9.en.aspx
ASKER
I tried that but am still having a freeze problem. Here is my form. It takes forever to download the first .jpg.file even though it's only small. The DL file is useless ( not openable). I tried increasing the antifreeze timeout to 1000 but it just hangs.
object Form1: TForm1
Left = 0
Top = 0
Width = 362
Height = 500
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 128
Top = 24
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object ComboBox1: TComboBox
Left = 96
Top = 72
Width = 145
Height = 21
ItemHeight = 13
TabOrder = 1
Text = 'ComboBox1'
end
object IdAntiFreeze1: TIdAntiFreeze
Left = 320
end
end
// Any thoughts on this or should I go to a webbrowser like you suggested ?
Elvin
object Form1: TForm1
Left = 0
Top = 0
Width = 362
Height = 500
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 128
Top = 24
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object ComboBox1: TComboBox
Left = 96
Top = 72
Width = 145
Height = 21
ItemHeight = 13
TabOrder = 1
Text = 'ComboBox1'
end
object IdAntiFreeze1: TIdAntiFreeze
Left = 320
end
end
// Any thoughts on this or should I go to a webbrowser like you suggested ?
Elvin
I never suggested a webbrowser.
and you got it wrong: the time should be decreased not increased. I worst cases I used 50 ms and changed the onlyWhenIdle to false.
this is the dfm for the unit posted by me earlier:
object Form1: TForm1
Left = 192
Top = 114
Width = 870
Height = 640
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 240
Top = 96
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object ComboBox1: TComboBox
Left = 376
Top = 96
Width = 145
Height = 21
ItemHeight = 13
TabOrder = 1
Text = 'ComboBox1'
end
object IdAntiFreeze1: TIdAntiFreeze
Left = 144
Top = 176
end
end
just for safety, here is the project as well: http://www.ciuly.com/delphi/DownloadMultipleFiles.zip
but since you are reffering to a hang, I am now considering your machine setup: are you using a proxy to access the internet? if so, then modify:
http:=TIdHTTP.Create(form);
to
http:=TIdHTTP.Create(form);
http.ProxyParams.ProxyPass word:=<you r proxy password if any>;
http.ProxyParams.ProxyPort :=<the proxy server port>;
http.ProxyParams.ProxyServ er:=<the proxy server address>;
http.ProxyParams.ProxyUser name:=<you r proxy username if any>;
to automatically detect IE proxy settings see this: http://www.mydesktophelp.com/tips/ie12.php
and you got it wrong: the time should be decreased not increased. I worst cases I used 50 ms and changed the onlyWhenIdle to false.
this is the dfm for the unit posted by me earlier:
object Form1: TForm1
Left = 192
Top = 114
Width = 870
Height = 640
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Button1: TButton
Left = 240
Top = 96
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 0
OnClick = Button1Click
end
object ComboBox1: TComboBox
Left = 376
Top = 96
Width = 145
Height = 21
ItemHeight = 13
TabOrder = 1
Text = 'ComboBox1'
end
object IdAntiFreeze1: TIdAntiFreeze
Left = 144
Top = 176
end
end
just for safety, here is the project as well: http://www.ciuly.com/delphi/DownloadMultipleFiles.zip
but since you are reffering to a hang, I am now considering your machine setup: are you using a proxy to access the internet? if so, then modify:
http:=TIdHTTP.Create(form);
to
http:=TIdHTTP.Create(form);
http.ProxyParams.ProxyPass
http.ProxyParams.ProxyPort
http.ProxyParams.ProxyServ
http.ProxyParams.ProxyUser
to automatically detect IE proxy settings see this: http://www.mydesktophelp.com/tips/ie12.php
ASKER
Just to clarify the Webbrowser...... //I suggest using a twebbrowser for displaying the adds// that's where I got that from. Anyhow I'll try the latest and post back on the results. Thanks dude !
Elvin
Elvin
oh. that I was reffering just to display, not download. but of course you can also use it for download but then you won't have the issue of download multiple files ;) The reason I suggested webbrowser for displaying is that you will be able to display mostly any type of banner the browser supports.
but in case of using webbrowser then you might think of leaving the multiple download issue out since you will only work with URLs directly (IE caches the files anyway so this will not put an issue in the case of files continuosly being downloaded ;) ) it's a matter of choiuse anyway.
you can still download all files with indy and then load the local file in the webbrowser: works as good.
but in case of using webbrowser then you might think of leaving the multiple download issue out since you will only work with URLs directly (IE caches the files anyway so this will not put an issue in the case of files continuosly being downloaded ;) ) it's a matter of choiuse anyway.
you can still download all files with indy and then load the local file in the webbrowser: works as good.
ASKER
Thanks Ciurly !
I think I will use the TWebBrowser. I just used the code to download the text file and loadup the combobox with the urls of the jpg files and have it rotate on a timer. As far as the firewall issue is concerned, I aren't behind any firewall so I'm not sure on why it is freezing up even when I use IdAntiFreeze component. I will play with it some more because I would still like to get multiple file downloads working for future reference. For now, I accept your answer.
Thanks again buddy.
Elvin
I think I will use the TWebBrowser. I just used the code to download the text file and loadup the combobox with the urls of the jpg files and have it rotate on a timer. As far as the firewall issue is concerned, I aren't behind any firewall so I'm not sure on why it is freezing up even when I use IdAntiFreeze component. I will play with it some more because I would still like to get multiple file downloads working for future reference. For now, I accept your answer.
Thanks again buddy.
Elvin
but: I suggest using a twebbrowser for displaying the adds. and this way you will be able to display gif, flash etc as well.
regarding the directrory download: if the http://somewebsite.com/files/ is not displaying a list of filles but some index.html with some otehr stuff on it, it will be almost impossible to do if you don't have a ftp access to that directory.