[fpc-pascal] Prorammatically sending file using HTTP POST?

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Sat Apr 7 11:20:51 CEST 2018


On 2018-04-07 10:13, Graeme Geldenhuys wrote:
> Have you tried this?
> 
>     http://lmgtfy.com/?q=delphi+indy+sending+file+via+http+post
> 
> I see many answers listed there. ;-)


And if you are really lazy... ;-)




Indy has TIdMultipartFormDataStream for this purpose:

procedure TForm1.SendPostData;
var
  Stream: TStringStream;
  Params: TIdMultipartFormDataStream;
begin
  Stream := TStringStream.Create('');
  try
   Params := TIdMultipartFormDataStream.Create;
   try
    Params.AddFile('File1', 'C:\test.txt','application/octet-stream');
    try
     HTTP.Post('http://posttestserver.com/post.php', Params, Stream);
    except
     on E: Exception do
       ShowMessage('Error encountered during POST: ' + E.Message);
    end;
    ShowMessage(Stream.DataString);
   finally
    Params.Free;
   end;
  finally
   Stream.Free;
  end;
end;


That will sent a file to the PHP script on the server.



Regards,
  Graeme

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp


More information about the fpc-pascal mailing list