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

有些话、适合烂在心里 提交于 2019-11-29 18:21:37

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

Actually, this is very simple to do, the way I've done it below.

I hope that by comparing your apps with the two below, you'll be able to figure out what you need to do to get yours to work correctly or, if not, it at least might help refine your q to focus on what the exact problem is.

To stop details like not having your data available here, and use of FMX and Live Bindings (and how you might be using them) getting in the way, I based my apps on the BioLife.CDS data you'll find in your Delphi Samples/Data folder. I based the code of the two apps on Malcolm Groves tutorial here

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

and in both apps I have a ClientDataSet, DataSource, DBGrid, DBNavigator and DBImage in each app, connected up exactly as you'd expect in a minimal db-aware-101 application.

The tethering mechanism sends the first app's CDS data to the second app as a stream, using the TClientDataSet SaveToStream and LoadFromStream methods.

The two apps worked first time with zero debugging.

App1 code:

  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;

Client code:

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;

Client 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 seem to work fine with LiveBindings, so if you are still having problems, it might be worth doing the data transfer in the same way as I have.

As for your second q

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

If you use a TClientDataSet to hold the data on your client (even if you are displaying it using some LiveBindings mechanism), you can save it on the client simply by calling the CDS's SaveToFile method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!