Delphi: 如何使用应用程序网络共享从服务​​器数据库中获取所有图像?

Delphi: How to Get All Images From Server Database by using App tethering?

大家好!

我有 客户端服务器

我正在使用 Delphi-xe8。应用程序 -> 多设备应用程序

客户端服务器都在使用应用程序网络共享,SQLite 数据库。

服务器向数据库添加图片时,完美添加,

但是在客户端点击"Get Image List"按钮。客户只收到一张图片[我想要所有图片]。

1. Quation: 如何从服务器数据库获取所有图像到客户端数据库。通过使用 App 网络共享 [SendStream]?我认为 AResource.Value.AsStream 的问题需要拆分,如何...???

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

客户端'Get Image List'按钮代码:

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;

How to Get All Images From Server Database by using App tethering?

其实这个做起来很简单,下面我就是这样做的。

我希望通过将您的应用程序与以下两个应用程序进行比较,您将能够了解 找出你需要做什么才能让你的工作正常,或者,如果没有,它至少可能会有所帮助 细化您的问题以专注于确切的问题所在。

停止详细信息,例如此处没有您的数据,以及使用 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 一起正常工作,所以如果您仍然遇到问题,可能值得像我一样进行数据传输。

至于你的第二个问题

How to copy all Images From Server Database and save on Client Created folder[Client\db\images] ?

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