Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. [SOLVED]Does XMLHttpRequest read local files, that are not declared in resources?

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

Scheduled Pinned Locked Moved QML and Qt Quick
3 Posts 2 Posters 9.9k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    glicanear
    wrote on 24 Sept 2014, 23:50 last edited by
    #1

    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?

    1 Reply Last reply
    0
    • R Offline
      R Offline
      Rolias
      wrote on 25 Sept 2014, 01:02 last edited by
      #2

      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.

      Check out my third course in the trilogy on Qt
      "Integrating Qt Quick with C++"
      http://bit.ly/qtquickcpp
      published by Pluralsight

      1 Reply Last reply
      0
      • G Offline
        G Offline
        glicanear
        wrote on 25 Sept 2014, 10:02 last edited by
        #3

        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.

        1 Reply Last reply
        0

        1/3

        24 Sept 2014, 23:50

        • Login

        • Login or register to search.
        1 out of 3
        • First post
          1/3
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved