繁体   English   中英

尝试在同一字段中发送HTTP多个文件时出现问题(Delphi)

[英]Trouble when try send HTTP multiple files in same field (Delphi)

我正在Delphi中创建一个项目,用于将多个文件上传到Web服务器,但是不能像在简单表单php上一样在同一字段上上传多个文件。 在我的Delphi代码下面:

procedure Http_arquivos;

var
  i: integer;
  arquivos: array [0..6] of String;
  HTTP: TIdHTTP;
  POSTData: TIdMultipartFormDataStream;

begin
  arquivos[0]:= 'c:\arquivo0.bmp';
  arquivos[1]:= 'c:\arquivo1.bmp';
  arquivos[2]:= 'c:\arquivo2.html';
  arquivos[3]:= 'c:\arquivo3.html';
  arquivos[4]:= 'c:\arquivo4.wav';
  arquivos[5]:= 'c:\arquivo5.bmp';
  arquivos[6]:= 'c:\arquivo6.txt';

  HTTP := TIdHTTP.Create(nil);
  POSTData := TIdMultipartFormDataStream.Create;

  for i:= 0 to Length(arquivos) do
  begin

    if fileexists (arquivos[i]) then 
    begin
      //showmessage(arquivos[i]);
      try
        POSTData.AddFile('files[]', arquivos[i], 'multipart/form-data');
        HTTP.Post('http://localhost/ENVIO/MultUp.php', POSTData);
      finally
        POSTData.Free;
    end;
  end;
end;
end;

您的代码有四个问题:

  1. 您的循环计数器是错误的。 您需要改用Length(arquivos)-1 (或Pred(Length(arquivos))High(arquivos) )。

  2. 您在循环内调用Post() ,但是它需要在循环外。

  3. 您为每个文件指定了错误的内容类型。

  4. 您将在每次循环迭代中破坏流。

尝试以下方法:

POSTData := TIdMultipartFormDataStream.Create;
try
  for i := Low(arquivos) to High(arquivos) do
  begin
    if FileExists(arquivos[i]) then 
    begin
      //AddFile() will choose the content type for you based on the file extension
      POSTData.AddFile('files[]', arquivos[i]);
    end;
  end;
  HTTP.Post('http://localhost/ENVIO/MultUp.php', POSTData);
finally
  POSTData.Free;
end;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM