Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Download file from server automatically? 1

Status
Not open for further replies.

ug505

Programmer
Jan 2, 2010
52
US
I've asked this question before and got no answers except how to upload which was of no help. I have a program that needs to download a text file from my server. It's basically a list of URLs my program is going to visit. Whenever you open the program, I want it to automatically start downloading the list so that when I update the list, the program (when opened) will download the new list. Is there any code that will download a file from my website to a folder on the users computer? (ex: to C:\TemporaryFolder) I can do FTP as well.
 
A lot depends on how nice/fancy/how many features you want. There's many components out there to do it and many ways to do it as well. The question is what kind of dependencies you want to have?

I got an example here using wininet. That's a Windows DLL that is more associated with Internet Explorer and gets updated then. You can do it in other ways too, like WinHTTP, and in a raw way using the TCP/IP sockets unit and certain ways are preferred. There's also two or three ways in WinInet, too, and probably likewise in other things. That aside, probably the simplest way I know to do it:

Code:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, wininet,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    SaveDialog1: TSaveDialog;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var
  hOpen: HINTERNET;
  hFile: HINTERNET;
  myAgent: string;
  savefile: file;
  amount_read: integer;
 // the buffer size here generally reflects maximum MTU size.
 // for efficiency sake, you don't want to use much more than this.
  mybuffer: array[1..1460] of byte;
begin
  if SaveDialog1.Execute then
    begin
      myAgent := 'Test downloader app';
     // other stuff in this call has to do with proxies, no way for me to test
      hOpen := InternetOpen(PChar(myAgent), 0, nil, nil, 0);
      if hOpen = nil then
        begin
          ShowMessage('Error in getting internet access' + SysErrorMessage(GetLastError));
          exit;
        end;
      try
        hFile := InternetOpenURL(hOpen, PChar(Edit1.Text), nil, 0,
             INTERNET_FLAG_RELOAD or INTERNET_FLAG_DONT_CACHE, 0);
        if hFile = nil then
          begin
            ShowMessage('Error in getting URL access' + SysErrorMessage(GetLastError));
            exit;
          end;
        try
          AssignFile(savefile, SaveDialog1.FileName);
          Rewrite(savefile, 1);
          InternetReadFile(hFile, @myBuffer, sizeof(mybuffer), amount_read);
          repeat
            Blockwrite(savefile, mybuffer, amount_read);
            InternetReadFile(hFile, @myBuffer, sizeof(mybuffer), amount_read);
          until amount_read = 0;
          CloseFile(savefile);
        finally
          InternetCloseHandle(hFile);
        end;
      finally
        InternetCloseHandle(hOpen);
    end;
  end;
end;

end.

Keep in mind depending on how you present the URL to it, you might have to do some other things to it like Canonicalize it or other things. You'll also want to thread it and send some kind of status to your form if you put too long of a download through it.

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Just use idHTTP:

var S: string;
begin
S := idHTTP1.Get(' // S now contains your list of URLS


[bobafett] BobbaFet [bobafett]
Code:
if not Programming = 'Severe Migraine' then
                       ShowMessage('Eureka!');
 
Hi guys

Is there a way off applying this code to enable a programme to full a file from a local server?

I am about to develop a no frills programme with the intention of allowing users to have the most up to date copy of master files on their pc's. Basically a superuser will update the masters on the server, then the users pc's will automatically pull the updated documents from the server and leave a copy on the machine.

Do you know of anything that could help me? The server and machines are windows based...

Thanks in advance.
Regards
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top