Vla*_*tin 1 delphi indy http-post indy10 delphi-10.1-berlin
我想用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;
Run Code Online (Sandbox Code Playgroud)
您可以使用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;
Run Code Online (Sandbox Code Playgroud)