应用环境:服务端:TROBinMessage,TROIndyHTTPServer
提供服务接口:
IBinaryMidService = interface
['{51904085-6D27-4EDA-A55D-A9EE21686894}']
function Add(Const ANum:AnsiString; const AType: AnsiString; const AValue: Binary): Integer;
end;
['{51904085-6D27-4EDA-A55D-A9EE21686894}']
function Add(Const ANum:AnsiString; const AType: AnsiString; const AValue: Binary): Integer;
end;
客户端:TROBinMessage,TROWinInetHTTPChannel,TRORemoteService
数据库:Oracle10,图片字段类型为BLOB
简单介绍:客户端把图片读入到流中并通过IBinaryMidService服务传输到服务器,然后有服务器存入到数据库中.
一:客户端生成流通过服务传输到服务器
1 procedure SavePic;
2 var
3 tmpStream:TROBinaryMemoryStream;
4 VBinaryMidService:IBinaryMidService;
5 begin
6 tmpStream:=TROBinaryMemoryStream.Create;
7 VBinaryMidService:=TBinaryMidService_Proxy.Create(ROMessage,ROChannel);
8 Image1.Picture.Graphic.SaveToStream(tmpStream) ;
9 VBinaryMidService.Add('0001',PicType,tmpStream);
10 end;
二:服务端接收流增加到数据库
function TBinaryMidService.Add(Const ANum: AnsiString; const AType: AnsiString; const AValue: Binary): Integer;
var
tmpSQLStr:string;
begin
tmpSQLStr:=format('insert into test(num,AType,pic) values (%s,%s,:0)',[quotedstr(ANum),quotedstr(AType)]);
DBOrder.AddPic(tmpSQLStr,AValue);
end;
//--------------ucDBOrder----------------------
function DBOrder.AddPic(ASQLStr:string;AValue)
var
QryTDB:TADOQuery;
begin
QryTDB:= TADOQuery.Create(nil);
QryTDB.Close;
QryTDB.SQL.Clear;
QryTDB.Connection:=Conn;
QryTDB.SQL.Add(SQLStr);
QryTDB.Parameters.ParamByName('1').LoadFromStream(AValue,ftBlob);
QryTDB.ExecSQL;
FreeAndNil(QryTDB);
end;
三:客户端得到数据并显示图片
View Code
1 procedure showPic;
2 var
3 Data:Variant;
4 VBinaryMidService:IBinaryMidService;
5 PicFile:TGraphic;
6 begin
7 //得到数据赋值给
8 VBinaryMidService:=TBinaryMidService_Proxy.Create (ROMessage,ROChannel);
9 VBinaryMidService.GetPic('00001',Data);
10 ClientDataSet.Data:=Data;
11 //显示图片
12 if not ClientDataSet1.FieldByName('Pic').IsNull then
13 begin
14 if ClientDataSet1.FieldByName('PicType').AsString='BMP' then
15 //显示bmp文件
16 PicFile:=TBitmap.Create;
17 else
18 if ClientDataSet1.FieldByName('PicType').AsString='JPG' then
19 begin
20 //显示JPG文件
21 PicFile:=TJPEGImage.Create;
22 end else
23 if ClientDataSet1.FieldByName('PicType').AsString='ICO' then
24 begin
25 //显示ICO文件
26 //IcoFile:=TIcon.Create; // IcoFile.Free;
27 PicFile:=TIcon.Create;
28 end else
29 if ClientDataSet1.FieldByName('PicType').AsString='PNG' then
30 begin
31 //显示PNG文件
32 PicFile:=TPNGObject.Create;
33 end else
34 if ClientDataSet1.FieldByName('PicType').AsString='GIF' then
35 begin
36 //显示PNG文件
37 PicFile:=TGIFImage.Create;
38 end;
39 mStream:=TMemoryStream.Create ;
40 TBlobField(ClientDataSet1.FieldByName('Pic')).SaveToStream(mStream);
41 mStream.Position :=0;
42 PicFile.LoadFromStream(MStream);
43
44 image1.Picture.Assign(PicFile);
45 PicFile.Free;
46 mStream.Free;
47 end;
48 end;
注:需下载gifimage.pas,pngimage插件(机器如果装了fastReport4,引用frxpngimage单元),Image控件才能支持PNG,GIF