Recent

Author Topic: [SOLVED] LNet, synapse or other way - How to simply make a http file download?  (Read 18164 times)

lazguy

  • Jr. Member
  • **
  • Posts: 78
Hello everybody!!!

I need to download a .zip file, a .pdf file and a text file from different http addresses on internet.

I downloaded LNet component but there is no example showing me how to do this I need.
I also downloaded Synapse component but I found the help confused and without examples, so i got confused.

I'm looking from something as simple as

httpfiledownload('http://somesite.com/somefile.zip','c:\somefolder\somefile.zip')

that work both from windows and linux, and, if possible, with no need of any external libraries or files.

If possible, if someone can post the code to do that i will be very thank.


Thank you very much in advance.
« Last Edit: February 08, 2012, 02:42:51 am by lazguy »

Laksen

  • Hero Member
  • *****
  • Posts: 802
    • J-Software
Using synapse you could use something like this:

Include httpsend and classes in the uses list
Code: [Select]
procedure DownloadFile(const Url, PathToSaveTo: string);
begin
   fs := TFileStream.Create(PathToSaveTo, fmOpenWrite or fmCreate);
   try
      HttpGetBinary(Url, fs);
   finally
      fs.Free;
   end;
end;

lazguy

  • Jr. Member
  • **
  • Posts: 78
Thank you very, very much Laksen !!!!!!!!!!!!

it is exactly what i need!!!

Problem Solved.

Almindor

  • Sr. Member
  • ****
  • Posts: 412
    • http://www.astarot.tk
In lNet you can use lnet/examples/console/lhttp/fpget

which is exactly like wget (less sophisticated but you get the point). I guess you missed it?

Lazaruszki

  • New Member
  • *
  • Posts: 10
Hi,

I use Synaps and this code (Lasken) for file download, but i can't use downloadsize value(httpsend.downloadsize) for progressbar at download time.

What is the solution? Need i use multithread? ...or another

If somebody can give an working example,  i will be very thank, too.


theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1946

Lazaruszki

  • New Member
  • *
  • Posts: 10
Hi Theo,

I try everything and searching with google but the result is pffffffff :o

Here is my full unit code, if you have a lot of time correct it, please:) O:-).

I like write a program that download setup.exe via http and drawing :) a progressbar.

I have 3 errors...

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls, ExtCtrls, Buttons, ComCtrls, ExtDlgs, EditBtn, FileCtrl,
  httpsend,blcksock,typinfo, synamisc;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    lblout: TLabel;
    Lblin: TLabel;
    Memo2: TMemo;
    ProgressBar1: TProgressBar;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ProgressBar1ContextPopup(Sender: TObject; MousePos: TPoint;
      var Handled: Boolean);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  SizeIn,SizeOut: integer;
implementation

{ TForm1 }


type

  THookSocketStatus = procedure(Sender: TObject; Reason: THookSocketReason;
    const Value: string) of object;

  TMemory=pointer;
   callback = class
       class procedure Status (Sender: TObject; Reason: THookSocketReason;
const Value: String);
       class procedure Monitor (Sender: TObject; Writing: Boolean; const
Buffer: TMemory; Len: Integer);
     end;


class procedure callback.Status(Sender: TObject; Reason: THookSocketReason;
  const Value: String);
var v: String;
begin

  v := getEnumName (typeinfo(THookSocketReason), integer(Reason)) + ' ' +
Value;

  Form1.Memo2.Lines.Add(v);

  if (reason=hr_readcount) then begin
                           inc(SizeIN,StrToInt64Def(Value,0));
                         //  ProgressBar1.Position := Round(1000 * (CurrentBytes / TotalBytes));
                           end;
  if (reason=hr_writecount) then
     inc(SizeOUT,StrToInt64Def(Value,0));

  Form1.lblIN.Caption :=inttostr(SizeIN);
  Form1.lblOUT.Caption:=inttostr(SizeOUT);

  application.ProcessMessages;

end;

class procedure callback.Monitor (Sender: TObject; Writing: Boolean; const
Buffer: TMemory; Len: Integer);
begin
  if not writing then
     inc(SizeIN, Len)
  else
     inc(SizeOUT, Len);

  Form1.lblIN.Caption :=inttostr(SizeIN);
  Form1.lblOUT.Caption:=inttostr(SizeOUT);

  application.ProcessMessages;

end;


procedure TForm1.Button1Click(Sender: TObject);

   var
  HTTP: THTTPSend;
  F: TFileStream;
  Start,Size: integer;
begin
  SizeIN:=0;
  SizeOUT:=0;

  Memo2.Lines.Add(CRLF+'GET');
  Memo2.Lines.Add('------------');

  Size:=HttpGetFileSize(Edit1.text); ///  ???

  HTTP := THTTPSend.Create;
  HTTP.Sock.OnStatus := callback.Status;

  //or
  HTTP.Sock.OnMonitor := callback.Monitor;


     F:=TFileStream.Create('hl.exe',fmcreate);

     HttpGetBinary('http://.../upgrade.exe',F);

     F.Free;



end;

procedure TForm1.FormCreate(Sender: TObject);
begin

end;

procedure TForm1.ProgressBar1ContextPopup(Sender: TObject; MousePos: TPoint;
  var Handled: Boolean);
begin

end;

initialization
  {$I unit1.lrs}

end.
     

Silvio Clécio

  • Guest

evoshroom

  • Full Member
  • ***
  • Posts: 157
I just wanted to say, this thread delivers!  :D :D :D

Fahmy Rofiq

  • Jr. Member
  • **
  • Posts: 83
Lazarus Trunk + FPC Fixes 32bit
Windows 10 x64

Silvio Clécio

  • Guest
Please see other demo for resuming download in LazSolutions framework:

https://github.com/silvioprog/lazsolutions

delphi887

  • New Member
  • *
  • Posts: 30
    • my personal website
hi
how do if i want get a file in multi connection in multi part and then join this parts to create file by synapse??? :)
thanks

 

TinyPortal © 2005-2018