Solved

Wininet help (downloading file)

Posted on 2003-10-27
5
570 Views
Last Modified: 2010-10-05
Hi.
I use Wininet for downloading files from internet.
When I start with downloading the file, my project is bloked (freezed).
Please paste this function in your project and compile it.Try to move the
form and you will se my problem (the form can be moved when you like).
Please tell me how can I solve ths problem.

uses Wininet;

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;



use:



var FileOnNet, LocalFileName: string
begin
 FileOnNet:=
  'http://www.clevercomponents.com/demo/inetsuite/v31/clInetSuiteDemoD6.exe';
 LocalFileName:='c:\windows\desktop\clInetSuiteDemoD6.exe'

 if GetInetFile(FileOnNet,LocalFileName)=True then
  ShowMessage('Download successful')
 else
  ShowMessage('Error in file download')

end;

Thank you

Nikola.


0
Comment
Question by:nikola_mk
5 Comments
 
LVL 17

Accepted Solution

by:
geobul earned 68 total points
ID: 9632418
Hi,

Try this:

...
  repeat
    Application.ProcessMessages; // add this line
    InternetReadFile(hURL, @Buffer,
                     SizeOf(Buffer), BufferLen);
    Application.ProcessMessages; // and this line
    BlockWrite(f, Buffer, BufferLen)
   until BufferLen = 0;
...

Regards, Geo
0
 
LVL 3

Assisted Solution

by:fibdev
fibdev earned 66 total points
ID: 9664226
Hi, I used to use Inet.  Didn't like it.  Try ICS It's free and powerful.
http://overbyte.delphicenter.com/frame_index.html

Place a httpCli component on your form and use code like this..

    Url := 'http://fibdev.com/bin/fpsetup.exe
    TrayIcon.Hint := 'Downloading Fibdev Fprint 3.0';

    if HTTPCli.State = httpReady then begin
    SI := TMemoryStream.Create;
    HTTPCli.URL := Url;
    try

      HTTPCli.RcvdStream := SI;

      sleep(500);
      HTTPCli.Get;
      // here SI contain the binary file
     SI.SaveToFile('.\fpsetup.exe');
     // You can launch it now or save that for later...
    // don't forget to free the memorystream when you're done
   SI.Free;
0
 
LVL 4

Assisted Solution

by:BedouinDN
BedouinDN earned 66 total points
ID: 9721086
Have you tried putting that function into another thread?
You could pass the URL file and the local file name to the thread as variables and call the function in the execute method of the thread and retrieve the value in the OnThreadTerminate event.
Works ok for me..
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question