2

How to convert the parameters of an HTML webform to use with TIdHTTP in Delphi?

I have a web server, with PHP, and a function to upload files. Via a web browser, I can send files without problems, but when I use TIdHTTP, it presents an error.

This is the HTML for the file upload page:

<html>
<head>
    <title>Upload de Arquivos com PHP</title>
</head>
<body>
     
<form method="post" action="recebe_upload.php" enctype="multipart/form-data">
<label>Arquivo:</label>
<input type="file" name="arquivo" />
<input type="submit" value="Enviar" />
</form>
     
</body>
</html>

This is the procedure I used to send the files:

procedure TForm1.Button1Click(Sender: TObject);
var
  Response, URL : String;
  LHTTPClient : TIdHTTP;
  Lista : TIdMultiPartFormDataStream;
  server, script, caminhoarq : string;
begin
  URL := 'http://192.168.15.101/RECEBE_UPLOAD.php\';
  Lista := TIdMultiPartFormDataStream.Create;
  HTTPClient := TIdHTTP.Create;
  HTTPClient.ProtocolVersion := pv1_1;
  HTTPClient.Request.ContentType := 'utf-8';
  HTTPClient.Request.UserAgent := 'Mozilla/3.0 (compatible;Indy Library)';
  HTTPClient.HTTPOptions := [hoForceEncodeParams, hoKeepOrigProtocol];
  Response := '';
  try
    try
      Lista.AddFormField('Arquivo','C:\Client\teste.txt');
      Response := UTF8Decode(Trim(HTTPClient.Post(URL, Lista)));
      memo1.Lines.add(response);
    except
        on e : exception do
          ShowMessage('Erro ao enviar arquivo ao servidor! Detalhes: '+e.Message);
    end;
  finally
    Lista.Free;
    HTTPClient.free;
  end;
end;

This is the error message I get:

<br />
<b>Notice</b>:  Undefined index: arquivo in <b>C:\xampp\htdocs\RECEBE_UPLOAD.php</b> on line <b>22</b><br />
<br />
<b>Notice</b>:  Undefined index: arquivo in <b>C:\xampp\htdocs\RECEBE_UPLOAD.php</b> on line <b>30</b><br />
<br />
<b>Strict Standards</b>:  Only variables should be passed by reference in <b>C:\xampp\htdocs\RECEBE_UPLOAD.php</b> on line <b>30</b><br />
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770

1 Answers1

4

You are not populating the TIdMultiPartFormDataStream correctly. You need to use the AddFile() method, not the AddFormField() method. The latter is meant for text fields instead.

Also, PHP's $_POST is case-sensitive when accessing webform fields. The HTML sends a field named arquivo, but your Delphi code is sending a field named Arquivo instead. That is why the PHP error says arquivo is unknown.

Also, the URL you are posting to is wrong, and the ContentType you are setting is wrong (though Post() will overwrite it).

Try this instead:

procedure TForm1.Button1Click(Sender: TObject);
var
  Response, URL : String;
  LHTTPClient : TIdHTTP;
  Lista : TIdMultiPartFormDataStream;
begin
  URL := 'http://192.168.15.101/recebe_upload.php';
  Lista := TIdMultiPartFormDataStream.Create;
  try
    Lista.AddFile('arquivo', 'C:\Client\teste.txt');

    HTTPClient := TIdHTTP.Create;
    try
      HTTPClient.ProtocolVersion := pv1_1;
      HTTPClient.HTTPOptions := [hoForceEncodeParams, hoKeepOrigProtocol];
      try
        Response := Trim(HTTPClient.Post(URL, Lista));
        Memo1.Lines.Add(Response);
      except
        on e : Exception do
          ShowMessage('Erro ao enviar arquivo ao servidor! Detalhes: ' + e.Message);
      end;
    finally
      HTTPClient.Free;
    end;
  finally
    Lista.Free;
  end;
end;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770