【问题标题】:How to convert the parameters of an HTML webform to use with TIdHTTP in Delphi?如何将 HTML webform 的参数转换为在 Delphi 中与 TIdHTTP 一起使用?
【发布时间】:2020-07-26 23:30:22
【问题描述】:

如何将 HTML webform 的参数转换为在 Delphi 中与 TIdHTTP 一起使用?

我有一个带有 PHP 的 Web 服务器和一个上传文件的功能。通过网络浏览器,我可以毫无问题地发送文件,但是当我使用 TIdHTTP 时,它会出现错误。

这是文件上传页面的 HTML:

<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>

这是我用来发送文件的过程:

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;

这是我收到的错误消息:

<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 />

【问题讨论】:

    标签: php delphi upload transfer idhttp


    【解决方案1】:

    您没有正确填充TIdMultiPartFormDataStream。您需要使用AddFile() 方法,而不是AddFormField() 方法。后者适用于文本字段。

    此外,PHP 的$_POST 在访问网络表单字段时区分大小写。 HTML 发送一个名为 arquivo 的字段,但您的 Delphi 代码发送的是一个名为 Arquivo 的字段。这就是为什么 PHP 错误提示 arquivo 未知的原因。

    另外,您发帖的 URL 错误,您设置的 ContentType 错误(尽管 Post() 会覆盖它)。

    试试这个:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-28
      • 1970-01-01
      • 1970-01-01
      • 2017-12-30
      • 1970-01-01
      • 2012-08-10
      • 2019-10-19
      • 2014-01-28
      相关资源
      最近更新 更多