Solved

Wininet help (downloading file)

Posted on 2003-10-27
5
571 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

809 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