【问题标题】:How to send a bitmap with HTTP post method如何使用 HTTP post 方法发送位图
【发布时间】:2017-04-24 11:41:20
【问题描述】:

我想使用 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;

【问题讨论】:

    标签: delphi http-post indy indy10 delphi-10.1-berlin


    【解决方案1】:

    您可以使用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 服务器将接受 multipart/form-data 提交而不是 application/x-www-webform-urlencoded 提交(代码的 TStringList 版本最初发布的内容)。
    猜你喜欢
    • 1970-01-01
    • 2011-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多