Is the listing already in a file or do you need to "scrape" a website URL to get the listing? Either way, one approach to get started is to just retrieve whatever is at a given URL. This is from the TMS WEB Core manual in the section on TWebHTTPRequest. To use it, drop a TWebHTTPRequest component on your form and add this to a button click event (for example).
And finally, there is also the promise/await based approach that permits writing code as if it is
sequential but still, underlying it is asynchronously executed.
procedure TForm1.WebButton1Click(Sender: TObject);
var
req: TJSXMLHttpRequest;
begin
WebHttpRequest1.URL := 'data.json';
try
req := await(TJSXMLHttpRequest, WebHttpRequest1.Perform());
showmessage(string(req.response));
except
// handle failure to execute request here
end;
end;
Note: do not forget to mark the method WebButton1Click() in the form declaration as async:
Here, string(req.response) should end up holding the contents of your listing, whether it is a directory or a file or whatever. You can put whatever you like in the URL, it doesn't have to be a link to a JSON file. Depending on what the response looks like, converting to JSON may be trivial or may take a bit of work. Do you have an example of what the response might look like?
Right, so if you use the above component, you should end up with a string that has the directory in it. This can then be parsed to get the filenames or sizes or whatever else is present in the directory and then converted to JSON or whatever else you might need. If you post a copy of what the response looks like, I (or someone else lurking about here) can help with converting that into JSON.
Part of the trouble is that different web servers and web server configurations format their output differently, so what you get back from TWebHTTPRequest needs to be reviewed carefully. Here's an example of how it might work. The result of clicking on the button in this example will be a TStringList containing all the filenames. Converting that into JSON is then not very difficult at all.
procedure TForm1.WebButton1Click(Sender: TObject);
var
req: TJSXMLHttpRequest;
search: String;
filename: String;
filelist: TStringList;
begin
WebHttpRequest1.URL := 'http://www.sfu.ca/images/';
try
req := await(TJSXMLHttpRequest, WebHttpRequest1.Perform());
search := string(req.Response);
filelist := TStringList.Create;
While pos('<a href=', search) > 0 do
begin
search := copy(search, pos('<a href="', search)+9, length(search));
filename := copy(search,1,pos('"',search)-1);
if pos('jpg',lowercase(filename)) > 0 then
begin
filelist.add(filename);
console.log(filename);
end;
end;
except
// handle failure to execute request here
end;
end;
If you point this at another URL, it might bring up different information as it is just parsing out the <a> links which, at the example URL of http://www.sfu.ca/images/, only contains the filenames in the directory. Another URL might have different headers or footers with their own <a> links or perhaps the files themselves might be presented differently than with an <a> tag.
So seeing an example of what you're working with would be a big help.
I am still getting the error:
I have the following in the .htaccess file
Header set Access-Control-Allow-Origin ""
Header set Access-Control-Allow-Origin=http://localhost:8000
Options +Indexes
I have added this to the Headers String list ( in the WebHttpRequest component )
Content-Type= text/plain
Accept=.*
Access to XMLHttpRequest at 'Index of /docs/' from origin 'http://localhost:8000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
Any ideas ????
I have a change of plan. I have used the following PHP file to get the filenames.
If I place the php file in the /docs folder I don't get any Cord errors.