Jump to content

TIniFile Question


Recommended Posts

Hi,

i have question concerning tinifile and how to load the content. My code looks like this:

type
  TIniFileHelper = class helper for TIniFile
    procedure LoadFromUrl(const AUrl:String);
  end;

procedure TIniFileHelper.LoadFromUrl(const AUrl:String);
var
  mRequest: TW3HttpRequest;
begin
  mRequest := TW3HttpRequest.Create;
  mRequest.OnDataReady:=procedure (Sender:TW3HttpRequest)
  begin
    self.FromString(Sender.ResponseText);
    TW3Dispatch.SetTimeout(Sender.free,100);
  end;
  mRequest.Get(AUrl);
end;

and i do something like this:

  ini := tinifile.create;  
 ini.LoadFromUrl('res/apps.ini')

If i put a showmessage( into the onDataReady i see the content, but it does not seem to be loaded into the ini class.

 

Christian

Link to post
Share on other sites
  • Moderators

(edited ) I would say either one of the following :

if you execute this from file, you will get an error message in the dev console something like this (on chrome)

"Access to XMLHttpRequest at 'file:///C:/.../res/apps.ini' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https"

and/or your get call should be replaced by something like

  var FHttp := TW3HttpRequest.Create;
  FHttp.OnDataReady := lambda writeln(FHttp.responsetext); end;
  FHttp.open('GET','res/textfile.txt');
  FHttp.send();

Edited :

your get call is quite ok, it wraps to open/get-writeheader/send anyway

if necessary, you might want to add a cors header

Access-Control-Allow-Origin: *
Link to post
Share on other sites

Thanks for the quick response. I do get "Uncaught TypeError: Cannot read property 'length' of undefined  [line #1198]" when i try to  read the context to self.FromString(Sender.ResponseText);  

 

type
  TIniFileHelper = class helper for TIniFile
    procedure LoadFromUrl(aUrl:String;Callback: TProcedureRef = nil);
  end;

procedure TIniFileHelper.LoadFromUrl(AUrl:String; Callback:TProcedureRef);
var
  mRequest: TW3HttpRequest;
begin
  mRequest := TW3HttpRequest.Create;
  mRequest.OnDataReady := procedure (Sender:TW3HttpRequest)
  begin
    showmessage(inttostr(Sender.ResponseText.length));
    ParseContent(Sender.ResponseText);
    if assigned(Callback) then Callback;
    TW3Dispatch.SetTimeout(Sender.free,100);
  end;
  mRequest.Get(aUrl);
end;

Showmessage shows the correct size of the Response string. ParseContent fails and the rest is not executed afterwards.

Link to post
Share on other sites
  • Administrators

@Christian can you attach the apps.ini -file? I've tested your code with a short apps.ini-file like this and it works:

[Test]
Foo=bar

My test code:

unit Form1;

interface

uses 
  System.Types, System.Types.Convert, System.Objects, System.Time,
  System.IOUtils, System.Device.Storage, System.IniFile, SmartCL.Net.Http,
  SmartCL.System, SmartCL.Time, SmartCL.Graphics, SmartCL.Components,
  SmartCL.FileUtils, SmartCL.Device.Storage, SmartCL.Forms, SmartCL.Fonts,
  SmartCL.Theme, SmartCL.Borders, SmartCL.Application;

type
  TForm1 = class(TW3Form)
  private
    {$I 'Form1:intf'}
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

  TIniFileHelper = class helper for TIniFile
    procedure LoadFromUrl(aUrl:String;Callback: TProcedureRef = nil);
  end;

implementation

{ TForm1 }

procedure TForm1.InitializeForm;
begin
  inherited;
  var ini:=TIniFile.Create;
  ini.LoadFromUrl('res/apps.ini',procedure
    begin
      WriteLn('Got the callback');
      WriteLn(ini.Read('Test','Foo'));
    end);
end;

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
end;
 
procedure TForm1.Resize;
begin
  inherited;
end;
 
procedure TIniFileHelper.LoadFromUrl(AUrl:String; Callback:TProcedureRef);
var
  mRequest: TW3HttpRequest;
begin
  mRequest := TW3HttpRequest.Create;
  mRequest.OnDataReady := procedure (Sender:TW3HttpRequest)
  begin
    WriteLn(Sender.ResponseText);
    ParseContent(Sender.ResponseText);
    if assigned(Callback) then Callback;
    TW3Dispatch.SetTimeout(Sender.free,100);
  end;
  mRequest.Get(aUrl);
end;

initialization
  Forms.RegisterForm({$I %FILE%}, TForm1);
end.

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...