Hi,
I'm trying to upload file with LHTTPClient. There is no demo in examples, so I'm doing the same way as with LTCPClient in this topic:
http://lazarus.firmos.at/index.php?topic=5146.0But I have problem with buffer. Above example presents how to send large text, so we can easily offset sended chunk with String[Position] index, but how do this with file buffer? I'm not familiar with streams. This is fragment of my code:
private
{ private declarations }
FFile: TFileStream;
FBuff: Pointer;
FBuffReaded, FBuffSended: Integer;
const
ChunkSize: Longint = 8192; { copy in 8K chunks }
procedure TForm1.FormCreate(Sender: TObject);
begin
FBuff := GetMem(ChunkSize);
FBuffReaded := 0;
FBuffSended := 0;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Freemem(FBuff);
FreeThenNil(FFile);
end;
procedure TForm1.http_clientCanWrite(ASocket: TLHTTPClientSocket;
var OutputEof: TWriteBlockStatus);
var
n: Integer;
begin
AddLog('Can write: ' + IntToStr(integer(OutputEof)));
OutputEof := wsPendingData;
repeat
// Next chunk if whole sended
if (FBuffSended>=FBuffReaded) and (FFile.Position<FFile.Size) then begin
FBuffReaded := FFile.Read(FBuff^, ChunkSize);
FBuffSended := 0;
if (FBuffReaded=0) then Break;
end;
n := ASocket.Send(FBuff^, FBuffReaded);
Inc(FBuffSended, n);
// Loop until filled the OS send buffer or sended whole file
until (n=0) or ((FBuffSended>=FBuffReaded) and (FFile.Position>=FFile.Size));
if (FBuffSended>=FBuffReaded) and (FFile.Position>=FFile.Size) then
OutputEof := wsDone;
end;
Line 39. How offset buffer (FBuff^) position to FBuffSended? Because in each loop cycle it send buffer from begining. Can I somehow strip first FBuffSended bytes from FBuff?
Regards and sorry for my English