Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Opening files from the internet

Status
Not open for further replies.
Detective,

You need to use controls or routines that support HTTP. For example, this loads the source of the web page specified in edit1.text into a Memo (with minimal error checking):

Code:
uses WinInet;
// ...
function getInternetFile( aURL : string;
                          aStream : tStream ) : Boolean;
const
   BUF_SIZE = 1024;
   OPEN_TYP = INTERNET_OPEN_TYPE_PRECONFIG;
var
   hiSession,
   hiConnect  : hInternet;
   achBuffer  : array[ 0 .. BUF_SIZE ] of char;
   cardCount  : cardinal;

begin

   result := FALSE;  // assume failure
   hiSession := internetOpen(nil, OPEN_TYP, nil, nil, 0);
   try

      hiConnect :=
         internetOpenURL( hiSession, pChar( aURL ), nil, 0, 0, 0 );
         try
            while TRUE do
            begin

               if not internetReadFile( hiConnect,
                  @achBuffer, sizeOf( achBuffer ),
                  cardCount ) then break;

               if cardCount = 0 then break
               else
                  begin
                     aStream.write( achBuffer, cardCount );
                     result := TRUE;
                  end;  // else

            end; // while

         finally
            internetCloseHandle( hiConnect );
         end; // try/finally

   finally
      internetCloseHandle( hiSession );
   end; // try/finally

end;

procedure TForm1.Button1Click(Sender: TObject);
var
   msFetch : TMemoryStream;
begin

   listbox1.Items.Clear;
   memo1.lines.clear;

   msFetch := tMemoryStream.create;
   screen.cursor := crHourglass;
   try
      if getInternetFile( edit1.text, msFetch ) then
         begin
            msFetch.position := 0;
            Memo1.Lines.loadFromStream( msFetch );
         end
      else
         Memo1.Lines.Text := 'Unable to fetch ' + edit1.text;
   finally;
      msFetch.free;
      screen.cursor := crDefault;
   end; // try/finally

end;

You can also creater and use a callback function to track the progress of the download. I'll leave that as an exercise. :)

Please note that if you simply want to display a web page in you r application, it may be best to simply use a TWebBrowser and pass the URL to its navigate method.

Hope this helps...

-- Lance

P.S. There are other ways to do this.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top