[SOLVED]Does XMLHttpRequest read local files, that are not declared in resources?



  • 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?



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.