Solved

Wininet help (downloading file)

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now