Recent

Author Topic: File Downloading  (Read 7427 times)

Lazerus

  • New member
  • *
  • Posts: 8
File Downloading
« on: August 03, 2010, 06:31:45 pm »
Hi, everyone!

How can I donwload file from server using Lazarus?

typo

  • Hero Member
  • *****
  • Posts: 3051
Re: File Downloading
« Reply #1 on: August 03, 2010, 06:45:08 pm »
ShellExecute(handle, 'open', PChar(DownloadableFileString), '','', SW_SHOWNORMAL); 

uses Windows.

Ocye

  • Hero Member
  • *****
  • Posts: 518
    • Scrabble3D
Re: File Downloading
« Reply #2 on: August 04, 2010, 09:21:22 am »
Quote
uses Windows
Pfui, use Synapse!

Code: [Select]
uses ...HTTPSend, blcksock;
type
  THttpFile=class(THttpSend)
    private
    public
      function Download(const aURL,aFileName:string):boolean;
    end;

function THttpFile.Download(const aURL,aFileName: string): boolean;
begin
  HTTPMethod('GET', aURL+aFileName);
  with TFileStream.Create(aFileName,fmCreate or fmOpenWrite) do
  try
    Seek(0, soFromBeginning);
    CopyFrom(Document, 0);
  finally
    Free;
  end;
end;

Take care of resultcode from HttMethod().
« Last Edit: August 04, 2010, 09:23:02 am by Ocye »
Lazarus 1.7 (SVN) FPC 3.0.0

fabienwang

  • Sr. Member
  • ****
  • Posts: 449
  • Lazarus is the best
    • My blog
Re: File Downloading
« Reply #3 on: August 05, 2010, 10:15:27 am »
Oh thanks Ocye i was exactly searching for the same.

Careful the latest Synapse methods changed, you will have to use HttpGetBinary
I'm using Arch Linux.
Known for: CPickSniff, OpenGrabby
Contributed to: LazPaint

mvaldez

  • New Member
  • *
  • Posts: 10
Re: File Downloading
« Reply #4 on: August 05, 2010, 12:43:49 pm »
With Indy 10, in Linux and Windows (and Mac?), I'd use something like:

Code: [Select]
uses IdHTTP, IdAntiFreeze;

FUNCTION Download_File (FileURL, FileFileName : STRING) : BOOLEAN;
VAR
  HTTPClient: TIdHTTP;
  Antifreeze : TIdAntiFreeze;
  FileStream : TMemoryStream;
BEGIN
  Download_File := FALSE;
    HTTPClient := TIdHTTP.Create (NIL);
    HTTPClient.HandleRedirects := TRUE;
    // If we are behind a proxy...
    //HTTPClient.ProxyParams.ProxyServer := '192.168.246.254';
    //HTTPClient.ProxyParams.ProxyPort := 800;
    Antifreeze := TIdAntiFreeze.Create (NIL);
    // If parameters are empty, do nothing...
    IF ((FileURL <> '') AND (FileFileName <> '')) THEN
      BEGIN
        // Remove target local file...
        SysUtils.DELETEFILE (FileFileName);
        // Encode URL...
        FileURL := HTTPClient.URL.URLEncode (FileURL);
        HTTPClient.Disconnect;
        FileStream := TMemoryStream.Create;
        TRY
          // Download the file...
          HTTPClient.Get (FileURL, FileStream);
          // Save the file...
          FileStream.SaveToFile (FileFileName);
          Download_File := TRUE;
        EXCEPT
        END;
        FileStream.Free;
      END;
    Antifreeze.Free;
    HTTPClient.Free;
END;

The .lpr (if using Linux) would need the unit cthreads as first unit, or you'll get a Runtime error.

And you may want to use TRY... FINALLY... blocks between every .Create and .Free procedures.


Regards,

MV

 

TinyPortal © 2005-2018