这是Indy自带的例子中的片段
//get selected image in listbox from server
procedure TfrmClient.btnGetSelectedImageClick(Sender: TObject);
var
ftmpStream : TFileStream;
begin
Try
if lstAvailableImages.itemindex = -1 then
MessageDlg('Cannot proceed until you select an image from the list !', mtInformation, [mbOK], 0)
else
with IdTCPClient do
begin
if connected then DisConnect;
Host := edtServerHost.text;
Port := StrToInt(edtServerPort.text);
Connect;
WriteLn('PIC:' + lstAvailableImages.Items[lstAvailableImages.itemindex]);
// delete if exists
// in production situation you might store binary downloads like this in a cache folder
if FileExists(ExtractFileDir(ParamStr(0)) + '/' + lstAvailableImages.Items[lstAvailableImages.itemindex]) then
DeleteFile(ExtractFileDir(ParamStr(0)) + '/' + lstAvailableImages.Items[lstAvailableImages.itemindex]);
ftmpStream := TFileStream.Create(ExtractFileDir(ParamStr(0)) + '/' + lstAvailableImages.Items[lstAvailableImages.itemindex],fmCreate);
while connected do
ReadStream(fTmpStream,-1,true);
FreeAndNil(fTmpStream);
Disconnect;
imgMain.Picture.LoadFromFile(ExtractFileDir(ParamStr(0)) + '/' + lstAvailableImages.Items[lstAvailableImages.itemindex]);
end;
except
on E : Exception do
ShowMessage(E.Message);
end;
end;