Delphi:如何使用App tethering从服务器数据库中获取所有图像?

时间:2017-02-09 15:10:54

标签: delphi firemonkey delphi-xe8

嗨,EveryBody!

客户服务器

我使用的是Delphi-xe8。应用程序 - >多设备应用程序

客户端服务器正在使用App tethering,SQLite数据库。

当服务器将图像添加到数据库时,它完美地添加了

Server Side

但在客户端,当客户点击"获取图像列表"按钮即可。客户只收到一张图片[我想要所有图片]。

Client Side

1。参数:如何将所有图像从服务器数据库获取到客户端数据库。通过使用App tethering [SendStream]?我认为AResource.Value.AsStream的问题需要Splite,How ...... ???

2。参数:如何从服务器数据库复制所有图像并保存在客户端创建的文件夹[Client \ db \ images]上?

客户'获取图片列表'按钮代码:

procedure TForm1.GetImgBtnClick(Sender: TObject);
begin
tAProfile.SendString(tManager.RemoteProfiles.First,'GetImages','get');
end;

服务器端:

procedure TForm2.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
  var
    MS:TMemorystream;
begin
if AResource.Hint='GetImages' then
        begin
        MS:=TMemorystream.Create;
         // ShowMessage(AResource.Value.AsString); // msg from client 'get'

        while not rQuery.Eof do
          begin
            tblobField(rQuery.FieldByName('image')).SaveToStream(MS1);
            Image1.Bitmap:=nil; // Если не занулить будет ошибка
         //   Image1.bitmap.LoadFromStream(MS);
            rQuery.Next;
          end;
          tAProfile.SendStream(tManager.RemoteProfiles.First,'SendImages',MS);  //Sending Images to Client MS
        end;
end;

客户端:

procedure TForm1.tAProfileResourceReceived(const Sender: TObject;
  const AResource: TRemoteResource);
begin
 if AResource.Hint='SendImages' then

  begin

  //  Image1.Bitmap.LoadFromStream(AResource.Value.AsStream);

    rQuery.Insert;
    TBlobField(rQuery.FieldByName('image')).LoadFromStream(AResource.Value.AsStream);
    rQuery.Post;
  end;

end;

1 个答案:

答案 0 :(得分:3)

  

如何使用App tethering从服务器数据库获取所有图像?

实际上,这很简单,就像我在下面这样做的那样。

我希望通过将您的应用与下面的两个应用进行比较,您将能够想到 你需要做些什么来让你的工作正常,如果没有,至少可能会有所帮助 优化你的q以专注于确切的问题。

要停止此处未提供数据的详细信息,以及使用FMX和Live Bindings (以及你如何使用它们)妨碍我将我的应用程序基于您在Delphi Samples / Data文件夹中找到的BioLife.CDS数据。我在这里基于Malcolm Groves教程的两个应用程序的代码

http://www.malcolmgroves.com/blog/?p=1854

在两个应用程序中,我在每个应用程序中都有ClientDataSet,DataSource,DBGrid,DBNavigator和DBImage, 完全按照您在最小的db-aware-101应用程序中的预期连接。

网络共享机制将第一个应用的CDS数据作为流发送到第二个应用, 使用TClientDataSet SaveToStreamLoadFromStream方法。

这两个应用程序第一次没有调试。

App1代码:

  TApp1Form = class(TForm)
    TetheringManager1: TTetheringManager;
    TetheringAppProfile1: TTetheringAppProfile;
    DBImage1: TDBImage;
    btnConnect: TButton;
    Label1: TLabel;
    CDS1: TClientDataSet;
    CDS1SpeciesNo: TFloatField;
    CDS1Category: TStringField;
    CDS1Common_Name: TStringField;
    CDS1SpeciesName: TStringField;
    CDS1Lengthcm: TFloatField;
    CDS1Length_In: TFloatField;
    CDS1Notes: TMemoField;
    CDS1Graphic: TGraphicField;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    DBNavigator1: TDBNavigator;
    btnSendStream: TButton;
    [...]
  end;

  [...]

procedure TApp1Form.btnConnectClick(Sender: TObject);
begin
  TetheringManager1.AutoConnect;
end;

procedure TApp1Form.btnSendStreamClick(Sender: TObject);
begin
  CDSToStream;
end;

procedure TApp1Form.FormCreate(Sender: TObject);
begin
  CDS1.Open;
  Caption := Format('App1 : %s', [TetheringManager1.Identifier]);
end;

procedure TApp1Form.TetheringManager1PairedToRemote(const Sender: TObject; const
    AManagerInfo: TTetheringManagerInfo);
begin
  Label1.Caption := Format('Connected : %s %s',
                         [AManagerInfo.ManagerIdentifier,
                          AManagerInfo.ManagerName]);
end;

procedure TApp1Form.CDSToStream;
var
  Stream : TMemoryStream;
begin
  Stream := TMemoryStream.Create;
  CDS1.SaveToStream(Stream);
  Stream.Position := 0;
  TetheringAppProfile1.Resources.FindByName('BioLife').Value := Stream;
end;

客户代码:

type
  TFmxApp2Form = class(TForm)
    CDS1: TClientDataSet;
    DataSource1: TDataSource;
    ImageControl1: TImageControl;
    BindingsList1: TBindingsList;
    BindNavigator1: TBindNavigator;
    BindSourceDB1: TBindSourceDB;
    LinkControlToField2: TLinkControlToField;
    TetheringManager1: TTetheringManager;
    TetheringAppProfile1: TTetheringAppProfile;
    StringGrid1: TStringGrid;
    Label1: TLabel;
    CDS1SpeciesNo: TFloatField;
    CDS1Category: TStringField;
    CDS1Common_Name: TStringField;
    CDS1SpeciesName: TStringField;
    CDS1Lengthcm: TFloatField;
    CDS1Length_In: TFloatField;
    CDS1Notes: TMemoField;
    CDS1Graphic: TGraphicField;
    LinkGridToDataSource1: TLinkGridToDataSource;
    procedure TetheringAppProfile1ResourceReceived(const Sender: TObject; const
        AResource: TRemoteResource);
    procedure TetheringManager1PairedFromLocal(const Sender: TObject; const
        AManagerInfo: TTetheringManagerInfo);
  private
  end;
[...]
procedure TFmxApp2Form.TetheringAppProfile1ResourceReceived(const Sender: TObject;
    const AResource: TRemoteResource);
begin
  AResource.Value.AsStream.Position := 0;
  CDS1.LoadFromStream(AResource.Value.AsStream);
end;

procedure TFmxApp2Form.TetheringManager1PairedFromLocal(const Sender: TObject; const
    AManagerInfo: TTetheringManagerInfo);
begin
 Label1.Text := Format('Connected : %s %s',
                        [AManagerInfo.ManagerIdentifier,
                         AManagerInfo.ManagerName]);
end;

客户DFM

object FmxApp2Form: TFmxApp2Form
  [...]
  object ImageControl1: TImageControl
    Bitmap.PNG = {}
  object BindNavigator1: TBindNavigator
    [...]
    DataSource = BindSourceDB1
  end
  object StringGrid1: TStringGrid
    [...]
  end
  object Label1: TLabel
    [...]
  end
  object CDS1: TClientDataSet
    Aggregates = <>
    FieldDefs = <
      item
        Name = 'Species No'
        DataType = ftFloat
      end
      item
        Name = 'Category'
        DataType = ftString
        Size = 15
      end
      item
        Name = 'Common_Name'
        DataType = ftString
        Size = 30
      end
      item
        Name = 'Species Name'
        DataType = ftString
        Size = 40
      end
      item
        Name = 'Length (cm)'
        DataType = ftFloat
      end
      item
        Name = 'Length_In'
        DataType = ftFloat
      end
      item
        Name = 'Notes'
        DataType = ftMemo
        Size = 50
      end
      item
        Name = 'Graphic'
        DataType = ftGraphic
      end>
    IndexDefs = <>
    Params = <>
    StoreDefs = True
    Left = 40
    Top = 32
    object CDS1SpeciesNo: TFloatField
      FieldName = 'Species No'
    end
    object CDS1Category: TStringField
      FieldName = 'Category'
      Size = 15
    end
    object CDS1Common_Name: TStringField
      FieldName = 'Common_Name'
      Size = 30
    end
    object CDS1SpeciesName: TStringField
      FieldName = 'Species Name'
      Size = 40
    end
    object CDS1Lengthcm: TFloatField
      FieldName = 'Length (cm)'
    end
    object CDS1Length_In: TFloatField
      FieldName = 'Length_In'
    end
    object CDS1Notes: TMemoField
      FieldName = 'Notes'
      BlobType = ftMemo
      Size = 50
    end
    object CDS1Graphic: TGraphicField
      FieldName = 'Graphic'
      BlobType = ftGraphic
    end
  end
  object DataSource1: TDataSource
    DataSet = CDS1
    Left = 104
    Top = 32
  end
  object BindingsList1: TBindingsList
    Methods = <>
    OutputConverters = <>
    Left = 40
    Top = 152
    object LinkControlToField2: TLinkControlToField
      Category = 'Quick Bindings'
      DataSource = BindSourceDB1
      FieldName = 'Graphic'
      Control = ImageControl1
      Track = False
    end
    object LinkGridToDataSource1: TLinkGridToDataSource
      Category = 'Quick Bindings'
      DataSource = BindSourceDB1
      GridControl = StringGrid1
      Columns = <>
    end
  end
  object BindSourceDB1: TBindSourceDB
    DataSet = CDS1
    ScopeMappings = <>
    Left = 40
    Top = 88
  end
  object TetheringManager1: TTetheringManager
    OnPairedFromLocal = TetheringManager1PairedFromLocal
    Text = 'TetheringManager1'
    AllowedAdapters = 'Network'
    Left = 40
    Top = 240
  end
  object TetheringAppProfile1: TTetheringAppProfile
    Manager = TetheringManager1
    Text = 'TetheringAppProfile1'
    Group = 'MAGroup'
    Actions = <>
    Resources = <
      item
        Name = 'BioLife'
        IsPublic = True
        Kind = Mirror
        ResType = Stream
        OnResourceReceived = TetheringAppProfile1ResourceReceived
      end>
    OnResourceReceived = TetheringAppProfile1ResourceReceived
    Left = 224
    Top = 240
  end
end

TClientDataSets似乎可以正常使用LiveBindings,因此如果您仍然遇到问题,可能需要以与我相同的方式进行数据传输。

至于你的第二个问题

  

如何从服务器数据库复制所有图像并保存在客户端创建的文件夹[Client \ db \ images]上?

如果您使用TClientDataSet来保存客户端上的数据(即使您使用某些LiveBindings机制显示它),只需调用CDS的SaveToFile方法即可将其保存在客户端上。