With these little modifications you can easily do this:

procedure TForm1.Button1Click(Sender: TObject);
> Var h: THTTPSend;
> begin
>   h := THTTPSend.Create;
>   h.HTTPMethodToFile('GET','http://pathtofile.zip','c:\testfile.zip');
>   h.Free;
>   Caption := 'ok';
> end;


modifications on httpsend.pas:

  THTTPSend = class(TSynaClient)
>   protected
>     FFileMode: Boolean;
>     FReceiveFile: TFileStream;
> ..
>   public
>     function HTTPMethodToFile(const Method, URL, RcvFileName: string):
> Boolean;
> ..
> constructor THTTPSend.Create;
> begin
>   inherited Create;
>   FFileMode := False;
> ..
> function THTTPSend.HTTPMethodToFile(const Method, URL, RcvFileName:
> string): Boolean;
> begin
>   FFileMode := True;
>   FFile := RcvFileName;
>   Result := HTTPMethod(Method, URL);
> end;
> ..
>
>
> function THTTPSend.HTTPMethod(const Method, URL: string): Boolean;
> ..
> begin
>   if FFileMode then FReceiveFile := TFileStream.Create(FFile,fmCreate or
> fmShareDenyWrite);
> ..
>   { connect }
>   if not InternalConnect(UpperCase(Prot) = 'HTTPS') then
>   begin
>     if FFileMode then FreeAndNil(FReceiveFile);
>     FFileMode := False;
> ..
>     { send Headers }
>     FSock.SendString(PrepareHeaders);
>     if FSock.LastError <> 0 then begin
>       if FFileMode then FreeAndNil(FReceiveFile);
>       FFileMode := False;
>       Exit;
>     end;
> ..
>   if FSock.LastError <> 0 then begin
>     if FFileMode then FreeAndNil(FReceiveFile);
>     FFileMode := False;
>     Exit;
>   end;
> ..
>         s := s + CRLF;
>         if FFileMode then
>           WriteStrToStream(FReceiveFile, s) else
>           WriteStrToStream(FDocument, s);
> ..
>   Result := FSock.LastError = 0;
>   if not Result then
>   begin
>     if FFileMode then FreeAndNil(FReceiveFile);
>     FFileMode := False;
>     FSock.CloseSocket;
> ..
>   ParseCookies;
>   if FFileMode then FreeAndNil(FReceiveFile);
>   FFileMode := False;
> end;
>
> function THTTPSend.ReadUnknown: Boolean;
> var
>   s: ansistring;
> begin
>   Result := false;
>   repeat
>     s := FSock.RecvPacket(FTimeout);
>     if FSock.LastError = 0 then
>       if FFileMode then
>         WriteStrToStream(FReceiveFile, s) else
>         WriteStrToStream(FDocument, s);
> ..
> function THTTPSend.ReadIdentity(Size: Integer): Boolean;
> begin
>   if Size > 0 then
>   begin
>     FDownloadSize := Size;
>     if FFileMode then begin
>       FSock.RecvStreamSize(FReceiveFile, FTimeout, Size);
>       FReceiveFile.Position := FReceiveFile.Size;
>     end else begin
>       FSock.RecvStreamSize(FDocument, FTimeout, Size);
>       FDocument.Position := FDocument.Size;
>     end;
>


On 15 April 2017 at 15:50, Benito van der Zander <ben...@benibela.de> wrote:

> Hi Brian,
>
> I build a "fake stream" for that. Derived from TMemoryStream, but actually
> just a wrapper around a basic TStream class.
> The only troublesome thing is that synapse uses the same string for up-
> and downloaded data
>
> Cheers,
> Benito
>
>
>
> On 04/15/2017 01:29 AM, brian - wrote:
>
> Other than that maybe you could just add a little modification to the
> HTTPSend class to work with TFileStream instead then you can avoid all that
> mess.
>
> On 15 April 2017 at 01:28, brian - <hikarito...@gmail.com> wrote:
>
>> You will need to work with the Status event, capturing data read and then
>> deciding a threshold to flush the buffer to a file and clear it
>>
>> I made a custom class/wrapper for this
>>
>> Type
>>    TOnReceiveData = procedure(Len,Max: Int64) of object;
>>
>> Var
>>     DownloadSize: Int64;
>>     xOnReceiveData: TOnReceiveData;
>>
>> Type TSynHTTP = class(TObject)
>>
>> Sock.OnStatus  := Status;
>>
>> //Callback function for status events
>> procedure TSynHTTP.Status(Sender: TObject; Reason: THookSocketReason;
>> const Value: String; const ValueInt: Int64);
>> Var V, currentHeader: String;
>>     i: integer;
>>   function GetSizeFromHeader(Header: String): integer;
>>   Var item: TStringList;
>>   begin
>>     //the download size is contained in the header (e.g.: Content-Length:
>> 3737722)
>>     Result := -1;
>>
>>     if Pos('Content-Length:', Header) <> 0 then begin
>>       item := TStringList.Create();
>>       item.Delimiter := ':';
>>       item.StrictDelimiter :=true;
>>       item.DelimitedText   :=Header;
>>       if item.Count = 2 then begin
>>         Result:= StrToInt(Trim(item[1]));
>>       end;
>>     end;
>>   end;
>> begin
>>   //try to get filesize from headers
>>   if Assigned(xOnReceiveData) then
>>   if (DownloadSize < 1) then begin
>>     for i := 0 to hSock.Headers.Count - 1 do begin
>>       currentHeader := hSock.Headers[i];
>>       DownloadSize  := GetSizeFromHeader(currentHeader);
>>       if DownloadSize <> -1 then break;
>>     end;
>>   end;
>>
>>   //HR_ReadCount contains the number of bytes since the last event
>>   if Reason = THookSocketReason.HR_WriteCount then begin
>>     if Assigned(xOnSendData) then xOnSendData(ValueInt);
>>   end;
>>   if DownloadSize > 0 then
>>   if Reason = THookSocketReason.HR_ReadCount then begin
>>     if Assigned(xOnReceiveData) then xOnReceiveData(ValueInt,Downlo
>> adSize);
>>   end;
>> end;
>>
>> So with this you will get a trigger as received data piles up, ValueInt =
>> amount of data received on this trigger, let's say 8 KB, then you capture
>> the event and sum up until you reach something like 50 MB, then write to a
>> file and clear the HTTPSend.Document stream, repeat until download is
>> finished.
>>
>> On 23 January 2017 at 17:45, Rainer Backes <rbac...@bond.de> wrote:
>>
>>> Hi,
>>>
>>> I would need a function/method to download a file via http/https
>>> directly to disk, without using the memory stream inside the THTTPSend
>>> class - the files that I have to download can be quite large (some GBs).
>>> Does anyone already have such a function ?
>>>
>>> Thanks
>>>
>>> Rainer
>>>
>>> ------------------------------------------------------------
>>> ------------------
>>> Check out the vibrant tech community on one of the world's most
>>> engaging tech sites, SlashDot.org! http://sdm.link/slashdot
>>> _______________________________________________
>>> synalist-public mailing list
>>> synalist-public@lists.sourceforge.net
>>> https://lists.sourceforge.net/lists/listinfo/synalist-public
>>>
>>>
>>
>
>
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
>
>
>
> _______________________________________________
> synalist-public mailing 
> listsynalist-public@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/synalist-public
>
>
>
> ------------------------------------------------------------
> ------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> synalist-public mailing list
> synalist-public@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/synalist-public
>
>
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
synalist-public mailing list
synalist-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/synalist-public

Reply via email to