Link to home
Start Free TrialLog in
Avatar of zafira12
zafira12

asked on

Downloading file from the net...

Hi. I'm trying the example bellow which works fine only with the HTTP address and " LocalFileName" specified by default; i would like to get files from everywhere and get their own names(followed by their  proper extension of course). in another words... save the files with their real names. Can someone give me a help ? Thanks !!

http://delphi.about.com/od/internetintranet/l/aa013001a.htm
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

You should be aware that for some url's, you will not be able to get the actual file name as it stored on the server. Take for example:

http://www.google.com

The result is an html page (.htm), but as to the actual file name? That becomes your choice as to what to save it as. For those urls that do specify a filename (url), then the following code will get the filename and use that when storing local. I also set the code so that if the filename is empty, it will default to "default.htm", as in the above google example:

function ExtractURLFileName(const FileName: string): string;
var  lpComponents:  TURLComponents;
     lpBuffers:     Array [0..5, 0..1024] of Char;
begin

  // Crack the url into components
  lpComponents.dwStructSize:=SizeOf(TURLComponents);
  lpComponents.lpszScheme:=@lpBuffers[0, 0];
  lpComponents.dwSchemeLength:=1024;
  lpComponents.lpszHostName:=@lpBuffers[1, 0];
  lpComponents.dwHostNameLength:=1024;
  lpComponents.nPort:=0;
  lpComponents.lpszUserName:=@lpBuffers[2, 0];
  lpComponents.dwUserNameLength:=1024;
  lpComponents.lpszPassword:=@lpBuffers[3, 0];
  lpComponents.dwPasswordLength:=1024;
  lpComponents.lpszUrlPath:=@lpBuffers[4, 0];
  lpComponents.dwUrlPathLength:=1024;
  lpComponents.lpszExtraInfo:=@lpBuffers[5, 0];
  lpComponents.dwExtraInfoLength:=1024;

  if InternetCrackUrl(PChar(FileName), Length(FileName), ICU_DECODE, lpComponents) then
  begin
     result:=lpBuffers[4];
     if (Length(result) = 0) then
        result:='default.htm'
     else if (ExtractFileExt(result) = EmptyStr) then
        result:=result+'.htm';
  end
  else
     result:='default.htm';

end;

function GetInetFile(URL, LocalPath: String): Boolean;
const BufferSize = 1024;
var
  hSession, hURL:   HInternet;
  Buffer:           Array [0..Pred(BufferSize)] of Byte;
  BufferLen:        DWORD;
  FileName:         String;
  f:                File;
begin

  hSession:=InternetOpen(PChar(ExtractFileName(Application.ExeName)), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  try
     hURL:=InternetOpenURL(hSession, PChar(URL), nil, 0, 0, 0);
     try
        FileName:=ExcludeTrailingBackslash(LocalPath)+'\'+ExtractURLFileName(URL);
        AssignFile(f, FileName);
        Rewrite(f, 1);
        repeat
           if InternetReadFile(hURL, @Buffer, SizeOf(Buffer), BufferLen) then
              BlockWrite(f, Buffer, BufferLen)
           else
              break;
        until (BufferLen = 0);
        CloseFile(f);
        result:=True;
     finally
        InternetCloseHandle(hURL);
     end
  finally
     InternetCloseHandle(hSession);
  end;

end;

// All that needs to be passed is the URL address and the local  path to save to

GetInetFile('http://www.google.com', 'c:\');

// this will save the file to
// c:\default.htm

Regards,
Russell


Avatar of kyrlean
kyrlean

Your code works great Russel. Thank you very much.Let me just make things clear in my head. All the addresses i tryed were saved as .htm .
There's no way i can provide the app an address and get the file right ??? (the original method presented by me above is excluded of course.....)
Another one... can you add some comments through the codes in order to better understand what has been done ??
Thanks again, and i apologize for asking too much !
ASKER CERTIFIED SOLUTION
Avatar of Russell Libby
Russell Libby
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great man. I tested it. This link for exemple got the file from ' http://delphi.about.com/library/forminbpl.zip"  as forminbpl.zip . Without any specification like => LocalFileName:='File Downloaded From the Net.zip' which is included on the example project at the web page  mentioned above.
Thank you very much !!!!!!!
I'll keep making efforts to become a great coder like you :-)   ! ! !

Thanks, and glad it does what you are after. ;-)

Russell