Re: Post image

Giganews Newsgroups
Subject: Re: Post image
Posted by:  Remy Lebeau (TeamB) (no.spam@no.spam.com)
Date: Sat, 14 Jul 2007

"Alexandre" <alexandrepires20…@yahoo.com.br> wrote in message
news:784E16F8B92DE340alexandrepires20…@yahoo.com.br...

> I would like to know how post a image with tidhttp.
> The type is: Content-type: image/jpeg

The same way you post anything else - with the Post() method.

Assuming the server is set up to accept JPG data directly, then simply set
the TIdHTTP's Request.ContentType property to 'image/jpeg', then load the
image data into a TStream, such as TFileStream or TMemoryStream, and pass it
to Post(), ie:

    var
        Strm: TMemoryStream;
    begin
        Strm := TMemoryStream.Create;
        try
            Strm.LoadFromFile('c:\path to\image.jpg');
            IdHTTP.Request.ContentType := 'image/jpeg';
            IdHTTP.Post('the url here', Strm);
        finally
            Strm.Free;
        end;
    end;

On the other hand, if the server is expecting you to send the image as a
file, such as if the user were filling in an input form on an HTML page,
then you need to pass a TIdMultipartFormDataStream to Post(), and don't set
the Request.ContentType property as that is done automatically, ie:

    var
        Strm: TIdMultipartFormDataStream;
    begin
        Strm := TIdMultipartFormDataStream.Create;
        try
            Strm.AddFile('TheFormsFieldNameForTheFile', 'c:\path
to\image.jpg', 'image/jpeg');
            // add other form fields if needed, ie:
            // Strm.AddFormField('TheFieldNameHere', 'TheValueHere');
            IdHTTP.Post('the url here', Strm);
        finally
            Strm.Free;
        end;
    end;

Gambit

Replies

None

In response to

Post image posted by Alexandre on Sat, 14 Jul 2007