Delphi 如何使用HTTP post方法发送位图

Delphi 如何使用HTTP post方法发送位图,delphi,http-post,indy,indy10,delphi-10.1-berlin,Delphi,Http Post,Indy,Indy10,Delphi 10.1 Berlin,我想用HTTPPOST方法发送位图图像。如何将其发送到URL 我正在使用Indy 10和Delphi 10.1。在一个过程中,我创建了一个包含所有参数值的TStringList,但我不知道如何传递位图数据 这是我的代码: procedure TuDm_Athlos.AddComandaInsertLogo(workList: TStringList; imageStream: TStream); var image : TBitmap; begin try image :=

我想用HTTP
POST
方法发送位图图像。如何将其发送到URL

我正在使用Indy 10和Delphi 10.1。在一个过程中,我创建了一个包含所有参数值的
TStringList
,但我不知道如何传递位图数据

这是我的代码:

procedure TuDm_Athlos.AddComandaInsertLogo(workList: TStringList;
  imageStream: TStream);
var
  image : TBitmap;
begin
  try
    image := TBitmap.Create;
    imageStream := TStream.Create;
    image.LoadFromFile('D:\\COFEE.BMP');
    image.SaveToStream(imageStream);
    workList.Add('db=titles');
    workList.Add('line_1=');
    worklist.Add('line_2=');
    workList.Add('line_3=');
    workList.Add('line_4=');
    workList.Add('line_5=');
    workList.Add('line_6=');
    workList.Add('store=&DB=PRN_UDG');
    workList.Add('code=1');
    workList.Add('width=' + IntToStr(image.Width));
    workList.Add('height=' + IntToStr(image.Height));
    workList.Add('length=576');
    workList.Add('store=');
  finally
    FreeAndNil(imageStream);
  end;
end;

function TuDm_Athlos.InsertLogo(imageStream: TStream;
  isFullResponse: Boolean): Boolean;
var
  StrResult     : UTF8String;
  workList      : TStringList;
  ContentStream : TStream;
  image         : TBitmap;
begin
  //Setup;
  Result := False;
  try
    try
      workList          := TStringList.Create;
      ContentStream     := TStream.Create;
      image             := TBitmap.Create;
      image.LoadFromStream(imageStream);
      AddComandaInsertLogo(workList,imageStream);
      AddComandaSummarize(workList, False);
      StrResult         := IdHTTP1.Post(printerURL + 'db_status.xml?',workList);
      ContentStream     := StringToStream(strResult);
      Result            := XmlReadCommanda(imageStream);   //XmlReadComanda(ContentStream);
    except
      on e : Exception do begin
        //DisconnectHttpClient;
        //raise Exception.Create(TranslateHttpError(e.Message));
      end;
    end;
  finally
    FreeAndNil(workList);
    FreeAndNil(image);
    ContentStream.Free;
  end;
end;

您可以使用
TIdMultiPartFormDataStream
而不是
TStringList
发送文件或流

uses
  ..., IdMultipartFormData;

procedure postImage(Url, FileName: String; imageStream: TStream);
var
  Form : TIdMultiPartFormDataStream;
  LStream: TStream;
begin
  if imageStream = nil then
    LStream := TIdReadFileExclusiveStream.Create(FileName)
  else
    LStream := imageStream;
  try
    Form := TIdMultiPartFormDataStream.Create;
    try
      Form.AddFormField('db', 'titles');
      Form.AddFormField('line_1', '');
      Form.AddFormField('line_2', '');
      Form.AddFormField('line_3', '');
      Form.AddFormField('line_4', '');
      Form.AddFormField('line_5', '');
      Form.AddFormField('line_6', '');
      Form.AddFormField('store', '');
      Form.AddFormField('DB', 'PRN_UDG');
      Form.AddFormField('code','1');
      Form.AddFormField('width', IntToStr(image.Width));
      Form.AddFormField('height',IntToStr(image.Height));
      Form.AddFormField('length','576');
      Form.AddFormField('store','');

      //CREATE A FIELD AND SET THE STREAM
      Form.AddFormField('bitmap', '', '', LStream, FileName);
      IdHTTP1.Post(Url, Form);
    finally
      Form.Free;
    end;
  finally
    if imageStream = nil then
      LStream.Free;
  end;
end;

procedure postImage(Url, FileName : String);
begin
  postImage(Url, FileName, nil);
end;

您可以使用
TIdMultiPartFormDataStream
而不是
TStringList
发送文件或流

uses
  ..., IdMultipartFormData;

procedure postImage(Url, FileName: String; imageStream: TStream);
var
  Form : TIdMultiPartFormDataStream;
  LStream: TStream;
begin
  if imageStream = nil then
    LStream := TIdReadFileExclusiveStream.Create(FileName)
  else
    LStream := imageStream;
  try
    Form := TIdMultiPartFormDataStream.Create;
    try
      Form.AddFormField('db', 'titles');
      Form.AddFormField('line_1', '');
      Form.AddFormField('line_2', '');
      Form.AddFormField('line_3', '');
      Form.AddFormField('line_4', '');
      Form.AddFormField('line_5', '');
      Form.AddFormField('line_6', '');
      Form.AddFormField('store', '');
      Form.AddFormField('DB', 'PRN_UDG');
      Form.AddFormField('code','1');
      Form.AddFormField('width', IntToStr(image.Width));
      Form.AddFormField('height',IntToStr(image.Height));
      Form.AddFormField('length','576');
      Form.AddFormField('store','');

      //CREATE A FIELD AND SET THE STREAM
      Form.AddFormField('bitmap', '', '', LStream, FileName);
      IdHTTP1.Post(Url, Form);
    finally
      Form.Free;
    end;
  finally
    if imageStream = nil then
      LStream.Free;
  end;
end;

procedure postImage(Url, FileName : String);
begin
  postImage(Url, FileName, nil);
end;

注意,这假设HTTP服务器将接受
多部分/表单数据
提交,而不是
应用程序/x-www-webform-urlencoded
提交(代码的
TStringList
版本最初发布的内容)。注意,这假设HTTP服务器将接受
多部分/表单数据
提交,而不是
应用程序/x-www-webform-urlencoded
提交(代码的
TStringList
版本最初发布的内容)。