Board index » delphi » sending a file through HTTP post ??

sending a file through HTTP post ??

hi guys,

i'm in big trouble. i have to write a program, that can upload a file via
HTTP post to a webserver. it's already working from a html page where i have
a form and a file input and sending it as "multipart/form-data" to a CGI
files on the webserver. now i have to do this with my program and i found
out, how to post arguments to the CGI file.

i'm using the idHTTP component like this:

var
  aStream: TMemoryStream;
  Params: TStringStream;
begin
  aStream:=TMemoryStream.Create;
  Params:=TStringSTream.Create('');
  HTTP.Request.ContentType:='application/x-www-form-urlencoded';
  try
    Params.WriteString('arg=value');
  except
    // whatever
  end;
  try
    FormAction.HTTP.Post('http://www.my-con.com/OT/otdb.taf', Params,
aStream);
  except
    on E: Exception do Result:=-1001; // Server nicht erreichbar
  end;
end;

but could anybody PLEASE tell me how to send file data like this ??

thanks a lot

 

Re:sending a file through HTTP post ??


Hi Sebastian,
Check this http://clevercomponents.com/articles/article009/httppost.asp
WBR
Sergey S,
Clever Components Team

Quote
"Sebastian Batzke" <Sebastian.Bat...@gmx.com> wrote in message

news:3ecf9afe@newsgroups.borland.com...
Quote
> hi guys,

> i'm in big trouble. i have to write a program, that can upload a file via
> HTTP post to a webserver. it's already working from a html page where i
have
> a form and a file input and sending it as "multipart/form-data" to a CGI
> files on the webserver. now i have to do this with my program and i found
> out, how to post arguments to the CGI file.

> i'm using the idHTTP component like this:

> var
>   aStream: TMemoryStream;
>   Params: TStringStream;
> begin
>   aStream:=TMemoryStream.Create;
>   Params:=TStringSTream.Create('');
>   HTTP.Request.ContentType:='application/x-www-form-urlencoded';
>   try
>     Params.WriteString('arg=value');
>   except
>     // whatever
>   end;
>   try
>     FormAction.HTTP.Post('http://www.my-con.com/OT/otdb.taf', Params,
> aStream);
>   except
>     on E: Exception do Result:=-1001; // Server nicht erreichbar
>   end;
> end;

> but could anybody PLEASE tell me how to send file data like this ??

> thanks a lot

Re:sending a file through HTTP post ??


Quote
"Sebastian Batzke" <Sebastian.Bat...@gmx.com> wrote in message <news:3ecf9afe@newsgroups.borland.com>...
> hi guys,

> i'm in big trouble. i have to write a program, that can upload a file via
> HTTP post to a webserver. it's already working from a html page where i have
> a form and a file input and sending it as "multipart/form-data" to a CGI
> files on the webserver. now i have to do this with my program and i found
> out, how to post arguments to the CGI file.

> i'm using the idHTTP component like this:

> var
>   aStream: TMemoryStream;
>   Params: TStringStream;
> begin
>   aStream:=TMemoryStream.Create;
>   Params:=TStringSTream.Create('');
>   HTTP.Request.ContentType:='application/x-www-form-urlencoded';
>   try
>     Params.WriteString('arg=value');
>   except
>     // whatever
>   end;
>   try
>     FormAction.HTTP.Post('http://www.my-con.com/OT/otdb.taf', Params,
> aStream);
>   except
>     on E: Exception do Result:=-1001; // Server nicht erreichbar
>   end;
> end;

> but could anybody PLEASE tell me how to send file data like this ??

> thanks a lot

I am currently developing an alternative method of using SQL servers
over the internet. Checkout http://www.dbnetsafe.com.

Edward Shave
dbNetSafe

Re:sending a file through HTTP post ??


Hi Sebastian,

here is some code using Indy 9.011 :

procedure FileUploadViaHTTP (FileNames : TStringList);
Const
  CRLF = #13#10;
  Separator = '---------------------------7d11fc42d01f0';
Var
  aStream    : TStringStream;
  Params     : TMemoryStream;
  S              : String;
  f               : TFileStream;
  i               : integer;

begin
    aStream   := TStringStream.create ('');
    Params    := TMemoryStream.Create;

    idHTTP1.Request.ContentType := 'multipart/form-data;
boundary='+Separator;

    for i := 0 to FileNames.Count-1 do
    begin
      S := '--'+Separator + CRLF +
           'Content-Disposition: form-data; filename="'+ExtractFileName
(FileNames [i])+'"'+CRLF+
           'Content-Type: application/octet-stream'+CRLF+CRLF;

      Params.Write(S[1], Length(S));
      f := TFileStream.Create (Filenames [i],fmopenread);
      Params.CopyFrom (f,0);
      f.free;
      Params.Write (CRLF [1],length (CRLF));
    end;

    S := '--'+Separator+'--'+CRLF+CRLF;
    Params.Write(S[1], Length(S));

    //Upload durchfhren und Ergebnis in aStream empfangen
    idHTTP1.Post ('http://www.my-con.com/OT/otdb.taf',Params,aStream);
end;

Hope that will help you.
I'm sorry that I couldn't give you an answer earlier, but I'm just back from
holidays.

Greetings

Michael

"Sebastian Batzke" <Sebastian.Bat...@gmx.com> schrieb im Newsbeitrag
news:3ecf9afe@newsgroups.borland.com...

Quote
> hi guys,

> i'm in big trouble. i have to write a program, that can upload a file via
> HTTP post to a webserver. it's already working from a html page where i
have
> a form and a file input and sending it as "multipart/form-data" to a CGI
> files on the webserver. now i have to do this with my program and i found
> out, how to post arguments to the CGI file.

> i'm using the idHTTP component like this:

> var
>   aStream: TMemoryStream;
>   Params: TStringStream;
> begin
>   aStream:=TMemoryStream.Create;
>   Params:=TStringSTream.Create('');
>   HTTP.Request.ContentType:='application/x-www-form-urlencoded';
>   try
>     Params.WriteString('arg=value');
>   except
>     // whatever
>   end;
>   try
>     FormAction.HTTP.Post('http://www.my-con.com/OT/otdb.taf', Params,
> aStream);
>   except
>     on E: Exception do Result:=-1001; // Server nicht erreichbar
>   end;
> end;

> but could anybody PLEASE tell me how to send file data like this ??

> thanks a lot

Other Threads