使用indy http方式上传文件

来源:互联网 发布:淘宝发空包会怎么样 编辑:程序博客网 时间:2024/05/21 09:30
 procedure TMainForm.Upload;
var
  MutPartForm: TIdMultiPartFormDataStream;
  i: Integer;
  FileItem: TFileItem;
  IdHTTP: TIdHTTP;
  sl: TStringList;
begin
  sl := TStringList.Create;
  for i := 0 to FFiles.Count - 1 do
  begin
    MutPartForm := TIdMultiPartFormDataStream.Create;
    IdHTTP := TIdHTTP.Create(nil);
    try
      try
        IdHTTP.HandleRedirects := True;
        FileItem := TFileItem(FFiles[i]);
        MutPartForm.AddFile('userfile', FileItem.FilePath, 'text/plain');
        IdHTTP.Post(FPostURL, MutPartForm);
        sl.Add('上传文件成功:' + FileItem.FilePath);
      except
      on E: Exception do
        begin
          sl.Add('上传文件失败:' + FileItem.FilePath + ' Message:' + E.Message);
        end;
      end;
    finally
      MutPartForm.Free;
      IdHTTP.Free;
    end;
  end;
  sl.SaveToFile(ExtractFilePath(Application.ExeName) + 'Post.log');
  sl.Free;
end;