🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.internet.socket
Article #1785

Re: Upload a file using PHP and http->Post ?

#1785
From: Remy Lebeau (TeamB)
Date: Thu, 26 Oct 2006 04:57
52 lines
2747 bytes
"Nicholas Wilson" <nicholasjohnwilson@gmail.com> wrote in message
news:45400f1d@newsgroups.borland.com...

> I am using the 9.15 version of the Indy component.

That is a very old version.  The current snapshot of Indy 9 is 9.0.50.  You
should consider upgrading.

> this is the sample code I cannot get to work.

It does not work because you are uploading the wrong stream to the server.
You are allocating a TMemoryStream named _params that you do not fill it
with any data but you ar uploading it anyway.  You are then allocating a
TIdMultiPartFormDataStream named _idmpfd that you are not uploading when you
should be.  You need to get rid of the _params stream altogether and upload
the _idmpfd stream instead.

Not only that, but you are filling in the TIdMultiPartFormDataStream
incorrectly anyway.  You should not be adding the file name and file data as
separate fields.  They belong to a single field in the HTML form, so you
have to add them to the stream as a single field.

Also, your code has memory leaks in it.  You need to free the streams you
allocate.

Try this instead:

    void __fastcall TForm1::Button1Click(TObject *Sender)
    {
        AnsiString _html;
        TIdMultiPartFormDataStream *_idmpfd = new
TIdMultiPartFormDataStream;
        try
        {
            ldhttp->Request->ContentType = _idmpfd->RequestContentType;

            // AddFile() calls GetMIMETypeFromFile() when no ContentType is
specified
            _idmpfd->AddFile("userfile", "formsource.h", "");

            _html = ldhttp->Post ("http://192.168.87.69/upload.php",
_idmpfd);
        }
        __finally {
            delete _idmpfd;
        }
        ShowMessage(_html);
    }


Gambit
Technical Details
Message-ID: <4540405a$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail
References: <4535c1b4@newsgroups.borland.com> <4535cae8@newsgroups.borland.com> <45400f1d@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 11508e,3eee350dd19e6632
X-Google-Attributes: gid11508e,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com>
Newsgroups: borland.public.cppbuilder.internet.socket
References: <4535c1b4@newsgroups.borland.com> <4535cae8@newsgroups.borland.com> <45400f1d@newsgroups.borland.com>
Subject: Re: Upload a file using PHP and http->Post ?
Date: Wed, 25 Oct 2006 21:57:16 -0700
Lines: 54
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1409
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409
NNTP-Posting-Host: 69.169.4.232
Message-ID: <4540405a$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1161838682 69.169.4.232 (25 Oct 2006 21:58:02 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!nx01.iad01.newshosting.com!newshosting.com!203.109.252.33.MISMATCH!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail