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
http://delphi.about.com/od/internetintranet/l/aa013001a.htm
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 !
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :-) ! ! !
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
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:
lpComponents.lpszScheme:=@
lpComponents.dwSchemeLengt
lpComponents.lpszHostName:
lpComponents.dwHostNameLen
lpComponents.nPort:=0;
lpComponents.lpszUserName:
lpComponents.dwUserNameLen
lpComponents.lpszPassword:
lpComponents.dwPasswordLen
lpComponents.lpszUrlPath:=
lpComponents.dwUrlPathLeng
lpComponents.lpszExtraInfo
lpComponents.dwExtraInfoLe
if InternetCrackUrl(PChar(Fil
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(PCh
try
hURL:=InternetOpenURL(hSes
try
FileName:=ExcludeTrailingB
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(hSessi
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