How To Send More Complex HTML Forms By HTTP Protocol

I have adapted the HTTPpostFile to allow you to upload the file and form fields together, instead of just the file. If you wish, add it to HTTPSend.pas

function HttpPostFileForm(const URL, FieldName, FileName: string;
  const Data: TStream; const FormFields:TStrings; const ResultData:
TStrings): Boolean;
var
  HTTP: THTTPSend;
  Bound, s: string;
  i:integer;
const
  FIELD_MASK = CRLF + '--%s' + CRLF +
              'Content-Disposition: form-data; name="%s"' + CRLF + CRLF+
              '%s';
begin
  Bound := IntToHex(Random(MaxInt), 8) + '_Synapse_boundary';
  HTTP := THTTPSend.Create;
  try
    s := '--' + Bound + CRLF;
    s := s + 'content-disposition: form-data; name="' + FieldName + '";';
    s := s + ' filename="' + FileName +'"' + CRLF;
    s := s + 'Content-Type: Application/octet-string' + CRLF + CRLF;
    HTTP.Document.Write(Pointer(s)^, Length(s));
    HTTP.Document.CopyFrom(Data, 0);
    // Include formfield
    for i:=0 to FormFields.Count-1 do
      begin
        S:= Format(FIELD_MASK,[Bound, FormFields.Names[I],
FormFields.Values[FormFields.Names[I]]]);
        HTTP.Document.Write(Pointer(S)^, Length(S));
      end;
    s := CRLF + '--' + Bound + '--' + CRLF;
    HTTP.Document.Write(Pointer(s)^, Length(s));
    HTTP.MimeType := 'multipart/form-data; boundary=' + Bound;
    Result := HTTP.HTTPMethod('POST', URL);
    ResultData.LoadFromStream(HTTP.Document);
  finally
    HTTP.Free;
  end;
end;

Regards, Eric Paschoalick Chaves

public/howto/htmlforms.txt · Last modified: 2012/01/27 11:56 by rasc
Driven by DokuWiki Recent changes RSS feed