Jump to content

Recommended Posts

  • Moderators

There are afaik 3 ways to read external files directly from the client :

  1. using ajax / xmlhttprequest
  2. using the filereader
  3. using the fetch api

1: get xmlhttprequest

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

2: file input

  W3EditBox1.InputType := itFile;
  W3EditBox1.Handle.ReadyExecute( procedure ()
    begin
      W3EditBox1.OnChanged := procedure(sender:TObject)
        begin
          var reader: variant;
          asm @reader = new FileReader(); end;
          reader.onload := lambda writeln(reader.result); end;
          reader.readAsText(W3EditBox1.handle.files[0]);
        end;
    end);

3: fetch api

var window   external 'window':   variant;
window.fetch('res/textfile.txt')
  .then(function(response:variant):variant begin
    result := response.text();
  end)
  .then(procedure(myText:variant) begin
    writeln(myText);
  end);

these fetch calls return a promise and the promise returns a response

The fetch api is a mechanism which can replace the usual ajax calls. This api is primarily designed to act on network events. (see this post), but works for simple file fetching as well

4: Variations of the above, see f.i. this one using webworkers

  var FileReader : variant := new JObject;
  asm @FileReader = new Worker('filereader.js'); end;
  FileReader.onmessage := procedure(e: variant)
  begin
    writeln(e.data);
  end;
  FileReader.postMessage('res/textfile.txt');     //read file through webworker

(filereader.js)

 

demo / project  (demo output is to console : ctrl+shift+i)

 

 

 

 

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...