How do I read in FILE contents in QML?

Christopher Peterson picture Christopher Peterson · Oct 14, 2011 · Viewed 21.2k times · Source

I just need something similar to Fstream to read file IO in QML. Why is there no file IO?

Answer

funkybro picture funkybro · Oct 16, 2011

If your file is plain text you can use XMLHttpRequest. For example:

var xhr = new XMLHttpRequest;
xhr.open("GET", "mydir/myfile.txt");
xhr.onreadystatechange = function() {
    if (xhr.readyState == XMLHttpRequest.DONE) {
        var response = xhr.responseText;
        // use file contents as required
    }
};
xhr.send();