[SOLVED]Does XMLHttpRequest read local files, that are not declared in resources?
-
wrote on 24 Sept 2014, 23:50 last edited by
I have modified code from "http://qmlbook.org/ch11/index.html#local-files":http://qmlbook.org/ch11/index.html#local-files and the result is:
@ function request() {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.HEADERS_RECEIVED) {
console.log('HEADERS_RECEIVED')
} else if(xhr.readyState === XMLHttpRequest.DONE) {
console.log('DONE');
console.log(xhr.responseText)
}
}
xhr.open("GET", "test.txt");
xhr.send();
}@result of program log is this::
qml: DONE
qml:Text file is not empty - it is located in the same folder as executable. Any ideas, what might be wrong?
Qt Quick Examples - XMLHttpRequest has data.xml file declared in resources, which is not what I'm after. Is it actually possible to read local files with XMLHttpRequest? If so, could someone share proper example?
-
wrote on 25 Sept 2014, 01:02 last edited by
Sure. Using that example code add the following right below the opening brace for the Rectangle in get.qml.
@
property url theFileName
FileDialog{
visible: true
onAccepted: {theFileName = fileUrl;
print(theFileName)}}
@
Then change the doc.open to use theFileName property:
@doc.open("GET", theFileName);@Now when you click on Get data, a file dialog will open. Navigate to where you have your .xml data file on disk and select it. Then click the Request data.xml button. You'll see the file url path printed in the console and the data should show up.
-
wrote on 25 Sept 2014, 10:02 last edited by
I see. That's not exactly what I wanted, as it is not going to work starting program with
@Component.onCompleted:@Apparently XMLHttpRequest can read local files directly when they are included in resources and since I'm updating that file with QFile, then I have to read it with QFile as well. The question was raised, because "http://qmlbook.org/ch11/index.html#local-files":http://qmlbook.org/ch11/index.html#local-files and other examples doesn't mention this restriction regarding location of these files.
3/3