Delphi XE2 DataSnap 上传下载文件

来源:互联网 发布:python贪吃蛇源代码 编辑:程序博客网 时间:2024/06/05 06:24

--------------------------------上传

服务器端:
function TServerMethods1.upPictureFile(f: TJSONArray): Boolean;
var
  t:TDBXJSONTools;
  s:TMemoryStream;
begin
  result:=false;
  try
  t:=TDBXJSONTools.Create;
  s:=TMemoryStream.Create;

  s:=t.JSONToStream(f) as TMemoryStream;
  s.SaveToFile('D:\Java学习笔记.doc');
  result:=true;
  finally
    freeandnil(t);
    freeandnil(s);
  end;
end;

客户端:
procedure TForm2.Button1Click(Sender: TObject);
var
  proxyServer:TServerMethods1Client;
  t:TDBXJSONTools;
  s:TMemorystream;
  f:TJSONArray;
begin
  if not SQLConnection1.Connected then  SQLConnection1.Connected:=true;
  try
  proxyServer:=TServerMethods1Client.Create(SQLConnection1.DBXConnection);
  t:=TDBXJSONTools.Create;
  s:=TMemoryStream.Create;

  s.LoadFromFile('E:\电子书\java\Java学习笔记.doc');
  //s.Position:=0;

  f:=t.StreamToJSON(s,0,s.Size);

  if proxyServer.upPictureFile(f) then

  showmessage(proxyServer.sayHello);
  finally
    freeandnil(proxyServer);
    freeandnil(t);
    freeandnil(s);
  end;
end;

 

----------------------------下载

服务器端:
function TServerMethods1.downPictureFiel(out f: TJSONArray): Boolean;
var
  t:TDBXJSONTools;
  s:TMemoryStream;
begin
  result:=false;
  try
  t:=TDBXJSONTools.Create;
  s:=TMemoryStream.Create;

  s.LoadFromFile('D:\3.jpg');
  s.Position:=0;

  f:=t.StreamToJSON(s,0,s.Size);

  result:=true;
  finally
    freeandnil(t);
    freeandnil(s);
  end;
end;

客户端:
procedure TForm2.bitbtn2Click(Sender: TObject);
var
  proxy:TServerMethods1Client;
  t:TDBXJSONTools;
  s:TMemoryStream;
  aFile:TJsonArray;
begin
  if not SQLConnection1.Connected then SQLConnection1.Connected:=true;
  proxy:=TServerMethods1Client.Create(SQLConnection1.DBXConnection);

  try
    t:=TDBXJSONTools.Create;
    s:=TMemoryStream.Create;
    aFile:=TJsonArray.Create;

    if proxy.downPictureFiel(aFile) then
      begin
        s:=t.JSONToStream(aFile) as TMemoryStream;
        s.Position:=0;
        s.SaveToFile('D:\down.jpg');
      end;

  finally
    freeandnil(t);
    freeandnil(s);
    freeandnil(aFile);
  end;


  showmessage('下载文件:'+proxy.sayHello);
end;

 

 


 

原创粉丝点击