HTTPS file upload and idhttp component

Giganews Newsgroups
Subject: HTTPS file upload and idhttp component
Posted by:  MikeR (NOnf4lSP…@nf4l.com)
Date: Sun, 01 May 2005

I need to upload a binary file to a third party server using https post, and the
specs in RFC1867.

I'm using Delphi7 and Indy 9. What is the meaning of the colored properties in the
property inspector (IOHandler, CookieManager and Intercept)? I have set none of
these properties. Is the actual usage documented somewhere? I'm really green on
this stuff.

Here's what I have so far, and it throws an exception on the post with the message
'IOHandler value is not valid'. The return variable, FMS is empty. Anyone know how
to resolve that? Beware line wrap.

procedure TfrmExpOpts.UploadFile(fnm: string);
var
  PMS, FMS: TMemoryStream;
  JFile: File of byte;
  URL, TFn, cmdparm, boundary, params, FileP, FileC, bak: string;
  OK: boolean;
  F: Textfile;
  fs: longint;
begin
  //Read file into FileC
  FileP := frmLog.AppDir + '\' + frmLog.CurrUser + '.xxx';
  AssignFile(JFile, FileP);
  Reset(JFile);
  fs := filesize(JFile);
  CloseFile(JFile);
  S := TFilestream.Create(FileP,fmOpenRead);
  Setlength(FileC, fs);
  s.ReadBuffer(FileC[1], fs);
  s.Free;
  boundary := '012345';
  params := '--' + Boundary + CCRLF;
  params := params + 'Content-Disposition: form-data; name="upfile";';
  params := params + ' FileName = "' + FileP + '"' +  CCRLF;
  params := params + 'Content-Type: text/plain' + CCRLF + CCRLF;
  params := params + FileC + CCRLF;
  params := params + '--' + Boundary + CCRLF;

  IdHTTP1.Request.ContentType := 'multipart/form-data; boundary="' + boundary + '"';
  PMS := TMemoryStream.Create;
  FMS := TMemoryStream.Create;
  PMS.Write(params[1],length(params));
  URL := 'https://www.someserver.org/varx/upload';
  try
    IdHTTP1.Post(URL, PMS,FMS);
  except
    on E: Exception do
    begin
      showmessage('Error encountered during POST: ' + E.Message);
      FMS.WriteBuffer(#0' ', 1);
      showmessage(PChar(FMS.Memory));
    end;
  end;
end;
Thanks,
MikeR

Replies