500分求助C/S下图片存入数据库问题(300分)

  • 主题发起人 主题发起人 blinking1313
  • 开始时间 开始时间
B

blinking1313

Unregistered / Unconfirmed
GUEST, unregistred user!
500分求助C/S下图片存入数据库问题。<br>我使用的是COM+,数据库是SQL,对数据库的操作放在服务器端。<br>客户端只能通过传递参数调用服务器端的方法。 <br><br>现在的问题是我要通过客户端向数据库保存图片。<br><br>如果是单机版,通常可以使用以下代码直接向数据库存图片,但现在改为网络版,如何才能把流或图片做为参数传递给服务端?<br>或是有其它办法。<br><br>var <br>&nbsp;testStream:TMemoryStream; <br>begin <br>&nbsp;try <br>&nbsp; &nbsp;testStream := TMemoryStream.Create; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //創建內存流 <br>&nbsp; &nbsp;Image1.Picture.Graphic.SaveToStream(testStream);  //將圖片保存至內存流中 <br>&nbsp; &nbsp;adoquery1.Close; <br>&nbsp; &nbsp;adoquery1.SQL.Clear; <br>&nbsp; &nbsp;adoQuery1.SQL.Add('Insert into test (id,photo) values (:id,:photo)'); //進行插入操作 <br>&nbsp; &nbsp;adoquery1.Parameters.ParamByName('id').Value := '003'; <br>&nbsp; &nbsp;adoQuery1.Parameters.ParamByName('photo').LoadFromStream(testStream,ftBlob); &nbsp;//讀取保存的內存圖 <br>&nbsp; &nbsp;adoquery1.ExecSQL; <br>&nbsp;finally <br>&nbsp; &nbsp;testStream.Free;   &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;//釋放內存流 <br>&nbsp;end;
 
能不能先把图片传到(用Ftp或其他的方法)服务器上的一个指定的位置,然后把那个路径作参数传给服务器处理?顶一个。
 
在服务端执行以上代码,并可以在客户端调用就可以了<br>这个问题也要500分^_^
 
数据库是sqlserver吗?直接往数据库里存就可以了呀,客户端跟单机的一样,没啥区别<br>你要是想存到服务器的目录下,不往数据库里放就得做个服务端用把图片上传了!
 
你的中间层应该有处理吧,把流做为参数传过去就可以了
 
我是想把流或图片做为参数传递给服务端,但不知选择哪种合适的参数类型。
 
在服务端定义个接口:<br>function &nbsp;TSoapDm.UpdateImage(PicStream: OleVariant; const StrSql: WideString;var OutPara: OleVariant):string safecall; <br>var<br>&nbsp; &nbsp;P : Pointer;<br>&nbsp; &nbsp;FileStream:TMemoryStream;<br>&nbsp; &nbsp;ADOQuery1:TADOQuery;<br>begin<br>&nbsp; FileStream:=TMemoryStream.Create; //生成内存流<br>&nbsp; ADOQuery1:=TADOQuery.Create(nil);<br>&nbsp; ADOQuery1.Connection:=ADOConnection1;<br>&nbsp; try<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp; &nbsp;FileStream.Size := VarArrayHighBound(PicStream, 1) - VarArrayLowBound(PicStream, 1) + 1;<br>&nbsp; &nbsp; &nbsp; &nbsp;P := VarArrayLock(PicStream);<br>&nbsp; &nbsp; &nbsp; &nbsp;FileStream.Position := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp;FileStream.Write(P^, fileStream.Size); //保存到流中<br>&nbsp; &nbsp; &nbsp; &nbsp;VarArrayUnlock (PicStream);<br>&nbsp; &nbsp; &nbsp; &nbsp;FileStream.Position := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp;with ADOQuery1 do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Close;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;with Parameters.AddParameter do<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;DataType:=ftVariant;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Direction:=pdInput;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Name:='ImgStream';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LoadFromStream(FileStream,ftVarBytes);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SQL.Clear;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SQL.Add(StrSql);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ExecSQL;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OutPara:=0;<br>&nbsp; &nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OutPara:=-1;<br>&nbsp; &nbsp; &nbsp; end;<br>&nbsp; finally<br>&nbsp; &nbsp; &nbsp;FreeAndNil(FileStream);<br>&nbsp; &nbsp; &nbsp;ADOQuery1.Free<br>&nbsp; end;<br>end;<br><br>然后在客户端这样调用:<br>var<br>&nbsp; fileStream: TFileStream;<br>&nbsp; sSql &nbsp; &nbsp; &nbsp;: String;<br>&nbsp; p &nbsp; &nbsp; &nbsp; &nbsp; : Pointer;<br>&nbsp; pic &nbsp; &nbsp; &nbsp; : Variant;<br>begin<br>&nbsp; fileStream:=TFileStream.Create(tmpFileName, fmOpenRead or fmShareDenyNone);<br>&nbsp; sSql:='update yourTable set imageField=:ImgStream ';<br>&nbsp; try<br>&nbsp; &nbsp; pic := VarArrayCreate ([0,fileStream.Size - 1], varByte);<br>&nbsp; &nbsp; P := VarArrayLock (Pic);<br>&nbsp; &nbsp; fileStream.Position := 0;<br>&nbsp; &nbsp; fileStream.Read (P^, fileStream.Size); //保存为 OleVariant<br>&nbsp; &nbsp; VarArrayUnlock (Pic);<br>&nbsp; &nbsp; (YourComInterface).UpdateImage(pic,sSql,outpara);<br>&nbsp; finally<br>&nbsp; &nbsp; fileStream.Free;<br>&nbsp; end;<br>end;
 
好像有点明白了。<br>还有200分我怎么给你?
 
后退
顶部