Reading a CSV file was enabled a while ago in Web Core, but I can't get it to work. I played all sorts of keyword bingo and the best I could achieve is this:
Var
Con : TWebClientConnection;
CDS : TWebClientDataSet;
Begin
Con := TWebClientConnection.Create(Self);
CDS := TWebClientDataSet.Create(Self);
Try
Con.AutoOpenDataSet := True;
CDS.Connection := Con;
Con.URI := 'myfile.csv';
Con.SkipFirstCSVLine := True;
Con.Delimiter := ';';
Await(Con.Open);
Console.log(CDS.RecordCount); // prints 5, as expected
CDS.First;
While not CDS.Eof do
Begin
Console.log(CDS.Fields[0].Asstring);
CDS.Next;
End;
This successfully loads the CSV file and CDS.RecordCount returns the number of rows in the CSV. All well up to this point!
Unfortunately, the first access to the fields as in
Console.log(CDS.Fields[0].Asstring);
throws exception "Cannot read properties of undefined (reading 'column0')"
What I tried:
- Con.AutoOpenDataSet := False;
Await(Con.Open);
Await(CDS.OpenAsync);
This hangs, OpenAsync does not resolve - Create the fields first
CDS.FieldDefs.Add('column0',ftString,255);
CDS.FieldDefs.Add('column1',ftString,255);
CDS.FieldDefs.Add('column2',ftString,255);
But this returns CDS.RecordCount = 0 - And many other combinations
What is the correct way to handle this such that CDS.Fields are assigned?