Delphi XE2 DataSnap 上传下载文件

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

服务器端:
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;

 

 


 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页