Results 1 to 11 of 11

Thread: [DELPHI] - Download Files

  1. #1

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    [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.

  2. #2
    Lively Member
    Join Date
    Feb 2005
    Posts
    120

    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'

  3. #3

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Re: [DELPHI] - Download Files

    did you include the ShellAPI in the uses clause?

  4. #4

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    Re: [DELPHI] - Download Files

    If it doesnt work ill rebuild an example project for you

  5. #5
    Lively Member
    Join Date
    Feb 2005
    Posts
    120

    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

    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.

  6. #6

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    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

  7. #7

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    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

  8. #8
    Lively Member
    Join Date
    Feb 2005
    Posts
    120

    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
    Allright!!
    Thx alot man

  9. #9

    Thread Starter
    Supreme User Madboy's Avatar
    Join Date
    Oct 2003
    Location
    England
    Posts
    3,253

    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

  10. #10
    Addicted Member
    Join Date
    Feb 2009
    Posts
    156

    Re: [DELPHI] - Download Files

    where is the download progress?!

    bad code.

  11. #11
    Fanatic Member BlindSniper's Avatar
    Join Date
    Jan 2011
    Location
    South Africa
    Posts
    865

    Re: [DELPHI] - Download Files

    Quote Originally Posted by kakablack View Post
    where is the download progress?!

    bad code.
    If you don't like the code why don't you just write it yourself?

    Useful CodeBank Entries of mine
    Expand Function
    Code Compiler
    Sudoku Solver
    HotKeyHandler Class

    Read this to get Effective help on VBForums
    Hitchhiker's Guide to Getting Help at VBF

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width